In Dlib how do I save image with overlay? - c++

I'm trying to modify Dlib's face detection example to save an image with detections to a file since I'm using a server without GUI. So far I have only figured how to save the image but not the overlay. How do I save both to the same file?
//win.add_overlay(dets, rgb_pixel(255,0,0));
save_png(img, "detected.png");

You can call draw_rectangle on the image before saving it.

Try this: dlib::draw_rectangle()
Example:
dlib::draw_rectangle(rect_image, rect, dlib::rgb_pixel(255, 0, 0), 1);

Related

How to open ECW file with GDAL in OpenCV 3.0

As you know, GDAL is added to OpenCV version 3. I have a satellite ecw image and want to read and show it. I already try to use OpenCV sample named : gdal-image.cpp. it has a line for reading input image:
cv::Mat image = cv::imread(argv[1], cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
my problem : I set my ecw image as argv[1] but it doesnt work.
should I convert my image before?
any way to read ecw using GDAL?
For read ecw image, i suggest you use GDAL, you can download GDAL binary prebuild and ECW extension from here https://www.gisinternals.com/release.php.
For read the first band of image you can use this code:
GDALDataset* dataset = (GDALDataset*) GDALOpenEx("image.ecw", GDAL_OF_RASTER, NULL, NULL, NULL);
GDALRasterBand* band = dataset->GetRasterBand(1);
cv::Rect roi(x, y, w, h);
cv::Mat mat;
mat.create(roi.size(),CV_32F);
band->RasterIO( GF_Read, roi.x, roi.y, roi.width, roi.height, mat.data,
roi.width, roi.height, GDT_Float32, 0, 0);
You just have to be sure that OpenCV data type match the GDAL datatype and that ROI dimensions are ok for the raster size.
For more info it is good: enter link description here
To read ECW file using GDAL,its driver should be built into the gdal, maybe you should test the supported driver in opencv. If the author didn't add the ecw driver, then do it youself. Here is the page about gdal in OpenCV:http://code.opencv.org/issues/3725
OpenCV's code repository was moved to github, and I can't find this issue, but in github i find something related here.

C++ (LINUX) set X11 window background with DevIL

I am trying to set a background image for one of my windows created with Xlib.
I would like the image to be a JPEG or PNG. I downloaded DevIL (which I prefer to use because it supports a lot of formats).
So, my questions, is, how do I do it? I can't find any specific tutorial or help.
I understand how I can use DevIL to load an image into a stream, but how do I put that on the window? I found an answer here: Load image onto a window using xlib but I don't know how and which function should receive the image bytes.
As I also understand, I should have a XImage that would hold all the image, and which I would use with XPutImage. What I don't understand is how do I send the image's bytes from DevIL to XImage.
Does somebody know any helpful page or maybe some clues about how I should do it?
Thanks!
The Xlib function used to create an XImage is XCreateImage, and its usage looks like this (you can read a full description in the link):
XImage *XCreateImage(display, visual, depth, format, offset, data,
width, height, bitmap_pad, bytes_per_line)
where the relevant argument for your specific question would be data, a char* that points to where you keep the image data loaded in with DevIL. With this, you should then be able to follow the steps in the other answer you already found.
Edited to add:
You still have to tell DevIL how to format your image data so that XCreateImage can understand it. For example the following pair of function calls will create an XImage that appears correctly:
ilCopyPixels(
0, 0, 0,
image_width, image_height, 1,
IL_BGRA, IL_UNSIGNED_BYTE,
image_data
);
// ...
XImage* background = XCreateImage(
display,
XDefaultVisual(display, XDefaultScreen(display)),
XDefaultDepth(display, XDefaultScreen(display)),
ZPixmap,
0,
image_data,
image_width,
image_height,
32,
0
);
, if you instead chose IL_RGBA, the colors will be off!

cv::Mat detect PixelFormat

I'm trying to use pictureBox->Image (Windows Forms) to display a cv::Mat image (openCV). I want to do that without saving the Image as a file ('cause i want to reset the image every 100ms).
I just found that topic here: How to display a cv::Mat in a Windows Form application?
When i use this solution the image appears to be white only. I guess i took the wrong PixelFormat.
So how do figure out the PixelFormat i need? Haven't seen any Method in cv::Mat to get info about that. Or does this depend on the image Source i use to create this cv::Mat?
Thanks so far :)
Here i took a screen. Its not completely white. So i guess there is some color info. But maybe i'm wrong.
Screen
cv::Mat.depth() returns the pixel type, eg. CV_8U for 8bit unsigned etc.
and cv::Mat.channels() returns the number of channels, typically 3 for a colour image
For 'regular images' opencv generally uses 8bit BGR colour order which should map directly onto a Windows bitmap or most Windows libs.
SO you probably want system.drawing.imaging.pixelformat.Format24bppRgb, I don't know what order ( RGB or BGR) it uses but you can use the opencv cv::cvtColor() with CV_BGR2RGB to convert to RGB
Thanks for the help! The problem was something else.
I just did not allocate memory for the Image Object. So there was nothing to really display.
Using cv::Mat img; in the headerFile and img = new cv::Mat(120,160,0); in Constructor (ocvcamimg Class) got it to work. (ocvcamimg->captureCamMatImg() returns img).

loadRawData Memory issue in ogre while load opencv frames

I am capturing images in real time using OpenCV, and I want to show these images in the OGRE window as a background. So, for each frame the background will change.
I am trying to use MemoryDataStream along with loadRawData to load the images into an OGRE window, but I am getting the following error:
OGRE EXCEPTION(2:InvalidParametersException): Stream size does not
match calculated image size in Image::loadRawData at
../../../../../OgreMain/src/OgreImage.cpp (line 283)
An image comes from OpenCV with a size of 640x480 and frame->buffer is a type of Mat in OpenCV 2.3. Also, the pixel format that I used in OpenCV is CV_8UC3 (i.e., each pixel is 8-bits and each pixel contains 3 channels ( B8G8R8 ) ).
Ogre::MemoryDataStream* videoStream = new Ogre::MemoryDataStream((void*)frame->buffer.data, 640*480*3, true);
Ogre::DataStreamPtr ptr(videoStream,Ogre::SPFM_DELETE);
ptr->seek(0);
Ogre::Image* image = new Ogre::Image();
image->loadRawData(ptr,640, 480,Ogre::PF_B8G8R8 );
texture->unload();
texture->loadImage(*image)
Why I always getting this memory error?
Quick idea, maybe memory 4-byte alignment issues ?
see Link 1 and
Link 2
I'm not an Ogre expert, but does it work if you use loadDynamicImage instead?
EDIT : Just for grins try using the Mat fields to setup the buffer:
Ogre::Image* image = new Ogre::Image();
image->loadDynamicImage((uchar*)frame->buffer.data, frame->buffer.cols, frame->buffer.rows, frame->buffer.channels(), Ogre::PF_B8G8R8);
This will avoid copying the image data, and should let the Mat delete it's contents later.
I had similar problems to get image data into OGRE, in my case the data came from ROS (see ros.org). The thing is that your data in frame->buffer is not RAW, but has a file header etc.
I think my solution was to search the data stream for the beginning of the image (by finding the appropriate indicator in the data block, e.g. 0x4D 0x00), and inserting the data from this point on.
You would have to find out were in your buffer the header ends and where your data begins.

save an image using SDL_image?

how can i save a SDL_image to an image file ..
i have an image loaded using SDL IMG_Load() method .. i want to now save it in file ??
i dont want to display it on the surface .. just want to load an image manipulate its pixels and save it back ... dont want to load it on front end ??
so how i can save it to the file ?
was written in some forum
'Not much, at least with SDL_image. SDL_image has only functions for
reading images, not for writing images. SDL has SDL_SaveBMP(), but it's hmm
just for BMPs. You will need to use another library for writing JPEGs.'
see here
thnks
SDL_image has an undocumented IMG_SavePNG() and IMG_SavePNG_RW(). The reason they are undocumented appears to be that they're limited to 32-bit RGBA images, but as this is far and away the most common format, it will likely be enough for your purposes.
You could use a third party library such as corona.
corona::Image* image = corona::OpenImage( "c:/filename.ext", corona::PF_R8G8B8A8 );
// do some stuff with the image...
corona::SaveImage( "c:/filename.ext", PF_R8G8B8A8, image );
delete image;