void testScale() { report(0,"checking scaling..."); ImageOf<PixelRgb> img; ImageOf<PixelMono> img2; ImageOf<PixelRgb> img3; img.resize(64,64); img.zero(); for (int i=0; i<img.width()/2; i++) { for (int j=0; j<img.height()/2; j++) { img(i,j).r = 255; img(i,j).g = 255; img(i,j).b = 255; } } img2.copy(img,32,32); checkEqual(img2.width(),32,"dimension check"); checkEqual(img2(0,0),255,"logic check"); checkEqual(img2(img2.width()-2,0),0,"logic check"); checkEqual(img2(0,img2.height()-2),0,"logic check"); img3.copy(img,16,16); checkEqual(img3.width(),16,"dimension check"); checkEqual(img3(0,0).r,255,"logic check"); checkEqual(img3(img3.width()-2,0).r,0,"logic check"); checkEqual(img3(0,img3.height()-2).r,0,"logic check"); img.copy(img3,4,4); checkEqual(img.width(),4,"dimension check"); }
static bool ImageReadRGB(ImageOf<PixelRgb> &img, const char *filename) { int width, height, color, num; FILE *fp=0; fp = fopen(filename, "rb"); if(fp==0) { fprintf(stderr, "Error opening %s, check if file exists.\n", filename); return false; } if (!ReadHeader(fp, &height, &width, &color)) { fclose (fp); fprintf(stderr, "Error reading header, is file a valid ppm/pgm?\n"); return false; } if (!color) { ImageOf<PixelMono> tmp; tmp.resize(width,height); const int w = tmp.width() * tmp.getPixelSize(); const int h = tmp.height(); const int pad = tmp.getRowSize(); unsigned char *dst = tmp.getRawImage (); num = 0; for (int i = 0; i < h; i++) { num += (int)fread((void *) dst, 1, (size_t) w, fp); dst += pad; } fclose(fp); img.copy(tmp); return true; } img.resize(width,height); const int w = img.width() * img.getPixelSize(); const int h = img.height(); const int pad = img.getRowSize(); unsigned char *dst = img.getRawImage (); num = 0; for (int i = 0; i < h; i++) { num += (int)fread((void *) dst, 1, (size_t) w, fp); dst += pad; } fclose(fp); return true; }
bool file::read(ImageOf<PixelRgba> & dest, const ConstString& src) { ImageOf<PixelRgb> img2; bool ok = ImageReadRGB(img2,src.c_str()); if (ok) { dest.copy(img2); } return ok; }
void testExternal() { report(0, "testing external image..."); unsigned char buf[EXT_HEIGHT][EXT_WIDTH]; { for (int x=0; x<EXT_WIDTH; x++) { for (int y=0; y<EXT_HEIGHT; y++) { buf[y][x] = 20; } } } ImageOf<PixelMono> img1; img1.setExternal(&buf[0][0],EXT_WIDTH,EXT_HEIGHT); checkEqual(img1.width(),EXT_WIDTH,"width check"); checkEqual(img1.height(),EXT_HEIGHT,"height check"); int mismatch = 0; for (int x=0; x<img1.width(); x++) { for (int y=0; y<img1.height(); y++) { img1.pixel(x,y) = 5; if (buf[y][x]!=5) { mismatch++; } } } checkEqual(mismatch,0,"delta check"); report(0, "testing various padding + alignments..."); for (int ww=1; ww<=17; ww++) { for (int hh=1; hh<=17; hh++) { for (int pad1=1; pad1<=9; pad1++) { for (int pad2=1; pad2<=9; pad2++) { int wwp1 = (ww%pad1)?(ww+pad1-(ww%pad1)):ww; FlexImage img; char *data = new char[wwp1*hh*3]; yAssert(data); img.setQuantum(pad1); img.setPixelCode(VOCAB_PIXEL_RGB); img.setPixelSize(3); img.setExternal(data,ww,hh); ImageOf<PixelRgb> target; target.setQuantum(pad2); target.copy(img); delete[] data; } } } } }
bool getVideo(ImageOf<PixelRgb>& image) { if (frameFinished) { FlexImage flex; flex.setPixelCode(VOCAB_PIXEL_RGB); flex.setQuantum((pFrameRGB->linesize[0])); flex.setExternal(pFrameRGB->data[0], pCodecCtx->width, pCodecCtx->height); image.copy(flex); } return frameFinished; }
/** * Read an image from the grabber. * * @param image The image to read. The image will be resized to * the dimensions the grabber is using, and the captured image * data will be written to it. * * @return True if an image was successfully captured. If false * returned, the image will be resized to the dimensions used by * the grabber, but all pixels will be zeroed. */ bool OpenCVGrabber::sendImage(IplImage* iplFrame, ImageOf<PixelRgb> & image) { // Resize the output image, this should not result in new // memory allocation if the image is already the correct size image.resize(iplFrame->width, iplFrame->height); if (!m_saidSize) { yDebug("Received image of size %dx%d\n", image.width(), image.height()); m_saidSize = true; } // Get an IplImage, the Yarp Image owns the memory pointed to IplImage * iplImage = (IplImage*)image.getIplImage(); // create the timestamp m_laststamp.update(); // Copy the captured image to the output image, flipping it if // the coordinate origin is not the top left if (IPL_ORIGIN_TL == iplFrame->origin) cvCopy(iplFrame, iplImage, 0); else cvFlip(iplFrame, iplImage, 0); if (iplFrame->channelSeq[0] == 'B') { cvCvtColor(iplImage, iplImage, CV_BGR2RGB); } if (m_w <= 0) { m_w = image.width(); } if (m_h <= 0) { m_h = image.height(); } if (fromFile) { if (m_w>0 && m_h>0) { if (image.width() != m_w || image.height() != m_h) { if (!m_saidResize) { yDebug("Software scaling from %dx%d to %dx%d", image.width(), image.height(), m_w, m_h); m_saidResize = true; } image.copy(image, m_w, m_h); } } } DBG yDebug("%d by %d %s image\n", image.width(), image.height(), iplFrame->channelSeq); return true; }
static bool ImageReadBGR(ImageOf<PixelBgr> &img, const char *filename) { int width, height, color, num; FILE *fp=0; fp = fopen(filename, "rb"); if(fp==0) { fprintf(stderr, "Error opening %s, check if file exists.\n", filename); return false; } if (!ReadHeader(fp, &height, &width, &color)) { fclose (fp); fprintf(stderr, "Error reading header, is file a valid ppm/pgm?\n"); return false; } if (!color) { fclose(fp); fprintf(stderr, "File is grayscale, conversion not yet supported\n"); return false; } ImageOf<PixelRgb> tmpImg; tmpImg.resize(width, height); const int w = tmpImg.width() * img.getPixelSize(); const int h = tmpImg.height(); const int pad = tmpImg.getRowSize(); unsigned char *dst = tmpImg.getRawImage (); num = 0; for (int i = 0; i < h; i++) { num += (int)fread((void *) dst, 1, (size_t) w, fp); dst += pad; } fclose(fp); return img.copy(tmpImg); }
/** * Read an image from the grabber. * * @param image The image to read. The image will be resized to * the dimensions the grabber is using, and the captured image * data will be written to it. * * @return True if an image was successfully captured. If false * returned, the image will be resized to the dimensions used by * the grabber, but all pixels will be zeroed. */ bool OpenCVGrabber::sendImage(const cv::Mat & frame, ImageOf<PixelRgb> & image) { // Resize the output image, this should not result in new // memory allocation if the image is already the correct size image.resize(frame.cols, frame.rows); if (!m_saidSize) { yDebug("Received image of size %zux%zu\n", image.width(), image.height()); m_saidSize = true; } // create the timestamp m_laststamp.update(); // Convert to RGB color space cv::Mat frame_rgb; cv::cvtColor(frame, frame_rgb, cv::COLOR_BGR2RGB); // Copy the captured image to the output image memcpy(image.getRawImage(), frame_rgb.data, sizeof(unsigned char) * frame_rgb.rows * frame_rgb.cols * frame_rgb.channels()); if (m_w == 0) { m_w = image.width(); } if (m_h == 0) { m_h = image.height(); } if (fromFile) { if (m_w>0 && m_h>0) { if (image.width() != m_w || image.height() != m_h) { if (!m_saidResize) { yDebug("Software scaling from %zux%zu to %zux%zu", image.width(), image.height(), m_w, m_h); m_saidResize = true; } image.copy(image, m_w, m_h); } } } DBG yDebug("%zu by %zu image\n", image.width(), image.height()); return true; }
bool file::write(const Image& src, const ConstString& dest) { int code=src.getPixelCode(); if (code==VOCAB_PIXEL_MONO) return write(static_cast<const ImageOf<PixelMono>&>(src),dest); else if (code==VOCAB_PIXEL_MONO_FLOAT) return write(static_cast<const ImageOf<PixelFloat>&>(src),dest); else if (code==VOCAB_PIXEL_BGR) return write(static_cast<const ImageOf<PixelBgr>&>(src),dest); else if (code==VOCAB_PIXEL_RGB) return write(static_cast<const ImageOf<PixelRgb>&>(src),dest); else if (code==VOCAB_PIXEL_RGBA) return write(static_cast<const ImageOf<PixelRgba>&>(src),dest); else { ImageOf<PixelRgb> img; img.copy(src); return write(img,dest); } }
void testCopy() { report(0,"testing image copying..."); ImageOf<PixelRgb> img1; img1.resize(128,64); for (int x=0; x<img1.width(); x++) { for (int y=0; y<img1.height(); y++) { PixelRgb& pixel = img1.pixel(x,y); pixel.r = x; pixel.g = y; pixel.b = 42; } } ImageOf<PixelRgb> result; result.copy(img1); checkEqual(img1.width(),result.width(),"width check"); checkEqual(img1.height(),result.height(),"height check"); if (img1.width()==result.width() && img1.height()==result.height()) { int mismatch = 0; for (int x=0; x<img1.width(); x++) { for (int y=0; y<img1.height(); y++) { PixelRgb& pix0 = img1.pixel(x,y); PixelRgb& pix1 = result.pixel(x,y); if (pix0.r!=pix1.r || pix0.g!=pix1.g || pix0.b!=pix1.b) { mismatch++; } } } checkTrue(mismatch==0,"pixel match check"); } }
void testCast() { report(0,"testing image casting..."); ImageOf<PixelRgb> img1; img1.resize(128,64); for (int x=0; x<img1.width(); x++) { for (int y=0; y<img1.height(); y++) { PixelRgb& pixel = img1.pixel(x,y); unsigned char v = x%30; pixel.r = v; pixel.g = v; pixel.b = v; } } ImageOf<PixelMono> result; result.copy(img1); checkEqual(img1.width(),result.width(),"width check"); checkEqual(img1.height(),result.height(),"height check"); if (img1.width()==result.width() && img1.height()==result.height()) { int mismatch = 0; for (int x=0; x<img1.width(); x++) { for (int y=0; y<img1.height(); y++) { PixelRgb& pix0 = img1.pixel(x,y); PixelMono& pix1 = result.pixel(x,y); if (pix0.r>pix1+1 || pix0.r<pix1-1) { mismatch++; } } } checkTrue(mismatch==0,"pixel match check"); } }
bool file::write(const Image& src, const ConstString& dest) { ImageOf<PixelRgb> img; img.copy(src); return write(img,dest); }