I converted IplImage to SDL_Surface
With the reference of this link
SDL_Surface *single_channel_ipl_to_surface (IplImage *opencvimg)
{
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,
opencvimg->width,
opencvimg->height,
opencvimg->depth*opencvimg->nChannels,
opencvimg->widthStep,
1, 1, 1 ,0);
return surface;
}
How can I convert SDL_Surface to IplImage
There are 4 pieces of information you'll have to retrieve from SDL_Surface, so investigate the API to know how you can retrieve:
The size of the image (width/height);
The bit depth of the image;
The number of channels;
And the data (pixels) of the image;
After that you can create an IplImage from scratch with cvCreateImageHeader() followed by cvSetData().
Related
I'm having an YUYV image buffer in cvMat object (snippet shown below). I had to convert this cvMat object to IplImage for color conversion.
CvMat cvmat = cvMat(480, 640, CV_8UC2, yuyv_buff);
I tried the below options to convert this cvmat object to IplImage object (src: https://medium.com/#zixuan.wang/mat-cvmat-iplimage-2f9603b43909 ).
//cvGetImage()
CvMat M;
IplImage* img = cvCreateImageHeader(M.size(), M.depth(), M.channels());
cvGetImage(&M, img); //Deep Copy
//Or
CvMat M;
IplImage* img = cvGetImage(&M, cvCreateImageHeader(M.size(), M.depth(), M.channels()));
//cvConvert()
CvMat M;
IplImage* img = cvCreateImage(M.size(), M.depth(), M.channels());
cvConvert(&M, img); //Deep Copy
But nothing worked. cvGetImage(), cvConvert() expects cvArr* as input. Passing &cvmat to them throws exception.
Is there any other way to convert an CvMat object to IplImage object in OpenCV 2.4 ?
Note: I cannot use C++ API or any other version of OpenCV. I'm limited to use only OpenCV 2.4
Edit 1: My objective is to convert this YUYV buffer to an RGB image object.
Instead of creating cvMat, I was able to create an IplImage directly from the yuyv image buffer like below:
IplImage* frame = cvCreateImage(cvSize(640,480), IPL_DEPTH_8U, 2);
frame->imageData=(char*)yuyv_buffer;
I want to convert any image I use as input to CV_32FC1 type. My code is below:
char fname[MAX_PATH];
while (openFileDlg(fname))
{
Mat img = imread(fname, CV_LOAD_IMAGE_UNCHANGED);
Mat src(img.rows, img.cols, CV_32FC1);
Mat dst(img.rows, img.cols, CV_32FC1);
if (img.type() == 0)
{
img.convertTo(img, CV_32FC1, 1.0f / 255.0f);
src = img.clone();
}
else
{
img.convertTo(img, CV_32FC1);
}
std::cout << img.type() << ' ' << src.type();
}
For grayscale images it works, but when I use a color image, the conversion doesn't work. For example: for CV_32FC1 the value is 5. When I upload a color image it gives me the value 16, and after conversion is 21. Any ideas?
You see inconsistent results for Gray and RGB images to the difference in channel numbers in both cases, 32FC1 can be broken down as:
32F - 32 bit floating value
C1 - Single channel
But RGB image or BGRA images have 3, 4 channels respectively, so we can't use C1 for them, hence we need to use 32FC3 for 3-channel image and 32FC4 for 4-channel image.
Currently I have the following:
[cameraFeed] -> [gaussianBlur] -> [sobel] -> [luminanceThreshold] -> [rawDataOutput]
the rawDataOuput I would like to pass it to the OpenCV findCountours function. Unfortunately, I can't figure out the right way to do this. The following is the callback block that gets the rawDataOuput and that should pass it to the OpenCV function but it does not work. I am assuming there are a few things involved such as converting the BGRA image given by GPUImage to CV_8UC1 (single channel) but I am not able to figure them out. Some help would be much appreciated, thanks!
// Callback on raw data output
__weak GPUImageRawDataOutput *weakOutput = rawDataOutput;
[rawDataOutput setNewFrameAvailableBlock:^{
[weakOutput lockFramebufferForReading];
GLubyte *outputBytes = [weakOutput rawBytesForImage];
NSInteger bytesPerRow = [weakOutput bytesPerRowInOutput];
// OpenCV stuff
int width = videoSize.width;
int height = videoSize.height;
size_t step = bytesPerRow;
cv::Mat mat(height, width, CV_8UC1, outputBytes, step); // outputBytes should be converted to type CV_8UC1
cv::Mat workingCopy = mat.clone();
// PASS mat TO OPENCV FUNCTION!!!
[weakOutput unlockFramebufferAfterReading];
// Update rawDataInput if we want to display the result
[rawDataInput updateDataFromBytes:outputBytes size:videoSize];
[rawDataInput processData];
}];
Try change CV_8UC1 to CV_8UC4 and then convert to gray.
In code replace lines
cv::Mat mat(height, width, CV_8UC1, outputBytes, step);
cv::Mat workingCopy = mat.clone();
with
cv::Mat mat(height, width, CV_8UC4, outputBytes, step);
cv::Mat workingCopy;
cv::cvtColor(mat, workingCopy, CV_RGBA2GRAY);
I have a One Image with depth of IPL_DEPTH_16S
IplImage result = cvCreateImage(cvGetSize(smoothImage), IPL_DEPTH_16S, 1);
cvSobel(smoothImage, result, 0, 1, 3);
and i want to pass that result image to other object which needs an IPL_DEPTH_8U image. So Is there any way to convert IPL_DEPTH_16S to IPL_DEPTH_8U in JavaCV.
I already try to use cvConvertScale() method. But i can't find what are the exact parametrs for that method.
Thankx..
Using the same style as your code, this should work:
IplImage i = cvCreateImage(cvGetSize(result), IPL_DEPTH_8U, 1);
cvConvertScale(result, i, 1, 0);
I have an array of RGB values from ImageMagick and I would like to create an IplImage structure in opencv. I was wondering how can I set the contents of the IplImage to be the rgb array values without writing the ImageMagick image to the hdd and then rereading it.
so you want to convert an ImageMagick Image object to an IplImage. It is just a matter of writing it to a buffer and then create an IplImage out of that buffer. The code from here (http://www.imagemagick.org/discourse-server/viewtopic.php?f=23&t=18183):
void Magick2Ipl(Image magicImage, IplImage* cvImage)
{
int width= magicImage.size().width();
int height = magicImage.size().height();
byte* blob= new byte[cvImage->imageSize];
magicImage.write(0,0, width, height, "BGRA", MagickCore::CharPixel, blob);
memcpy(cvImage->imageData, blob, cvImage->imageSize);
delete [] blob;
}
Best regards,
Daniel
You can do it by this way also,
Image image("Image_Path");
int width = image.size().width();
int height = image.size().height();
IplImage* mat = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
image.write(0, 0, width, height, "BGR", Magick::CharPixel, (char*)mat->imageData);
cvShowImage("image", mat);