ios - How to send IplImage from server to iPod client UIImage via TCP -
i have server in linux using berkeley_sockets , create tcp connection ipod client. have iplimage* img;
send server ipod. use write(socket,/*data*/,43200);
command , data tried send is: reinterpret_cast<char*>(img)
, img
, img->imagedata
. of choices send kind of data.
on ipod side receive data way (as i've seen here in so. don't mind complicated stuff, it's receiving data single image.):
bytesread = [istream read: (char*)[buffer mutablebytes] + totalbytesread maxlength: 43200 - totalbytesread];
after receiving whole image, have this:
[buffer setlength: 43200]; nsdata *imagem = [nsdata datawithbytes:buffer length:43200]; uiimage *final= [self uiimagefromiplimage:imagem];
now.. know have opencv working on ipod, can't find simple explanation on how work, used the second code webpage , adapted it, since know specifications of image (for instance set variables cgimagecreate()
function.):
- (uiimage *)uiimagefromiplimage:(nsdata *)image { cgcolorspaceref colorspace = cgcolorspacecreatedevicegray(); // allocating buffer cgimage nsdata *data = [nsdata datawithbytes:image length:43200]; cgdataproviderref provider = cgdataprovidercreatewithcfdata((cfdataref)data); // creating cgimage chunk of iplimage size_t width = 240; size_t height = 180; size_t depth = 8; //bitspercomponent size_t depthxnchannels = 8; //bitsperpixel size_t widthstep = 240; //bytesperrow cgimageref imageref = cgimagecreate(width, height, depth, depthxnchannels, widthstep, colorspace, kcgimagealphanone|kcgbitmapbyteorderdefault,provider, null, false, kcgrenderingintentdefault); // getting uiimage cgimage uiimage *ret = [uiimage imagewithcgimage:imageref]; lolview.image = ret; cgimagerelease(imageref); cgdataproviderrelease(provider); cgcolorspacerelease(colorspace); return ret;
}
the problem: when display image, weird , ´random´, though image sent same. have no idea what's wrong..
ps: tcp connection working fine other data, numbers or words. , image grayscale.
thanks help.
i got working this. on server side (code::blocks in linux openframeworks (& ofxopencv)):
img.allocate(240, 180, of_image_color); //ofimage img2.allocate(240, 180); //ofxcvcolorimage frame = cvcreateimage(cvsize(240,180), ipl_depth_8u, 3); //iplimage bw = cvcreateimage(cvsize(240,180), ipl_depth_8u, 1); //iplimage gray.allocate(240, 180); //ofxcvgrayscaleimage ///ofimage img.loadimage("lol.jpg"); ///ofimage -> ofxcvcolor img2.setfrompixels(img.getpixels(), 240, 180); ///ofxcvcolor -> iplimage frame = img2.getcvimage(); ///iplimage in gray cvcvtcolor(frame,bw,cv_rgb2gray); cvthreshold(bw,bw,200,255,cv_thresh_binary); //it binary image gray = bw; pix = gray.getpixels(); n=write(newsockfd,pix,43200);
on client side (ipod 4.3):
-(uiimage *) datafromiplimagetouiimage:(unsigned char *) rawdata; { size_t width = 240; size_t height = 180; size_t depth = 8; //bitspercomponent size_t depthxnchannels = 8; //bitsperpixel size_t widthstep = 240; //bytesperrow cgcontextref ctx = cgbitmapcontextcreate(rawdata, width, height, depth, widthstep, cgcolorspacecreatedevicegray(), kcgimagealphanone); cgimageref imageref = cgbitmapcontextcreateimage (ctx); uiimage* rawimage = [uiimage imagewithcgimage:imageref]; cgcontextrelease(ctx); myimageview.image = rawimage; return rawimage; free(rawdata); }
probably there's easier way this, hey, gets work done. hope helps anyone.
Comments
Post a Comment