using opencv in iOS - c++

I want to firstly choose a picture from my the local library, it shows on the UIImageView
and then I can use the opencv fuction UIImageToMat() to convert the UIImage to Mat and then I convert Mat to IPlImage to implement image processing algorithms(why I have to convert Mat to IplImage? Because most of my opencv image processing codes are based on IplImage).
So the choosen image shows on the UIImageView, if I want to do some image processing things, I'll first convert the UIImage to IplImage, process it and then convert IplImage to UIImage again. That's the way I use opencv to do image processing things.
But the problem is when I want to process the chosen picture from the local library, something strange happens:
the original picture is:
and then I want to use opencv to let some columns of this picture to be blue and some rows to be red, but the result is:
the implementation code is:
I don't know why this happens, the picture obviously lost many important features during the opencv image processing process.(I just want some columns to be blue and some rows to be red.).Why this happened?

Related

Why is my Qt5 QPixmap not showing correctly frames processed with some OpenCV algorithms?

Thank you in advance for your support.
I am using OpenCV for processing video frames taken by a video camara, and showing the processed frames in a simple GUI implemented in Qt5. In the GUI, the images are shown using QPixmap in a label. The OpenCV algorithms should be right, since if I write the outputs are right, and they are basically some examples provided by OpenCV.
I have implemented different processings: For a conversion from color to grey scale, and for binary threshold (see image 1) the results are fine (this "view" of the camera is right). Nevertheless, when trying to display ("in real time") Keypoints detections (using SURF -see image 2-) and contours detections (using Canny -see image 3-), the images displayed are strange.
The main problem is they seem to be at the same time "much closer" (see 2) and double (see 3).
In the Qt code I am using:
ui->labelView->setScaledContents(true);
I do the conversion from the processed OpenCV frame to QImage using:
QImage output((const unsigned char*) _frameProcessed.data, _frameProcessed.cols, _frameProcessed.rows, QImage::Format_Indexed8);
And I display the image using:
ui->labelView->setPixmap(QPixmap::fromImage(frame));
The GUI and the OpenCV processing are running in different threads: I move the image processing to a thread in an initial setup.
If you needed further information please just let me know.
Thank you very much in advance!
Best regards,
As #Micka pointed out, it had to do with the formats of the images. I found this handy code providing functions for an automatic transformation between OpenCv and Qt formats.

Read DICOM in C++ and convert to OpenCV

I would like to read DICOM images in C++ and manipulate them using opencv.
I managed to read a dicom image using DCMTK however, I am unsure how to convert it to an opencv Mat.
The following is what I have so far:
DicomImage DCM_image("test.dcm");
cv::Mat image(int(DCM_image.getWidth()), int(DCM_image.getHeight()), CV_8U, (uchar*)DCM_image.getOutputData(8));
which results in the following:
In a DICOM viewer, it looks as follows:
After normalising, the grayed image appears as follows:
Any help would be greatly appreciated.
The main problem I can see now is the difference of pixel value ranges (depths).
AFAIK, DICOM can have a rather big depth (16 bit), and you are trying to fit in into CV_8U, which is only 8 bit. You can get DicomImage instance's depth using DicomImage::getDepth(), and then create a cv::Mat with appropriate depth to hold your image data.
You may also need to normalize the data to maximally utilize you available range, so that the display with cv::imshow() would look as expected.
So:
Do DicomImage::getDepth() on your DCM_image
Create a cv::Mat with sufficient depth to hold your data
Scale, if necessary
Before calling DicomImage::getOutputData() on a monochrome DICOM image, you should make sure that you've selected an appropriate VOI transformation (e.g. Window Center & Width). This could be done by DicomImage::setMinMaxWindow(), DicomImage::setWindow() etc. See documentation of the DicomImage class.
Please note, however, that DicomImage::getOutputData() always returns rendered pixel data, i.e. not the original Pixel Data that is stored in the DICOM dataset.
U need to read the data type in DICOM image encoding an convert that to opencv type Mat. The opencv docs provides the entire information on their Mat header.

Replace white background of image with transparent using C++ / OpenCV or Matlab

Using C++ / OpenCV or Matlab I'm trying to find a method to replace the white background of an image from file with a transparent background and then save the image for further use elsewhere. (The image is similar to something like this: http://www.psdgraphics.com/file/monitor-on-white-background.jpg)
I understand this task can be completed easily with simple image editing software but I need to do this for a large batch of images. I've tried methods such as Make white background transparent png matlab as well as a bunch of others with no luck.
Thanks!
Load the image. Loading images in OpenCV
Convert it to a 4 channel image. OpenCV: transforming 3 channel image into 4 channel
Determine where the background is adjust the alpha channel of those pixels accordingly. I don't know what your images look like, but if there is no other white in your images, it's a fairly simple task. (If a pixel is white, then it should be transparent.) Otherwise, if there is other white in your images, then you need to look for the contiguous region(s) of white that make up the background. See Flood Fill Algorithm
Save the image (See the link from 1.)

Importing and looping through a PNG image to check for transparency

I would like to write CPP code that is able to take in a PNG file, scan through its pixels and identify where the transparent pixels are.
I tried doing this with CIMG, but it didn't work out as CIMG only supports the RGB channels. Even after installing image magick, the 4th channel is not giving me the right values.
Anyone can suggest a library I could use?

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).