convert mat to ipl image in opencv 3.0 - c++

I tried to convert mat image to IplImage but i could not able to convert it , i tried like this
Mat frame=imread("image path");
IplImage* image=IplImage(frame);
I got the error like cannot convert 'IplImage {aka _IplImage}' to 'IplImage* {aka _IplImage*}' in initialization...please any one tell how to convert in opencv 3.0

Related

Transform a YUV420p QVideoFrame into grayscale OpenCV mat

I've created a filter extending QAbstractVideoFilter and
QVideoFilterRunnable and I've overrided the
QVideoFrame run(QVideoFrame* input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags)`
method
The problem is that QVideoFrame format is Format_YUV420P and has no handle. I need to convert it into a CV_8UC1 in order to use OpenCV algorithms.
Which is the best way to accomplish this?
First you need to create a cv::Mat which has an API for initializing using data pointer as:
cv::Mat img = cv::Mat(rows, cols, CV_8UC3, input.data/*Change this to point the first element of array containing the YUV color info*/)
Now since the img is initialized with YUV color data, you may use various cvtColor modes to convert the YUV mat to other formats, for converting it to gray-scale you may try:
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_YUV2GRAY_I420);

How to normalize Mat .with 1/255, where Mat is of OpenCV type

I am using OpenCV, firstly i converted UIImage into Mat format & further
i want to normalize Mat with 1/255(divide each element of Mat with 1/255).
currently i am using
outputImageMat.convertTo(outputImageMat, CV_32F, 1.0 / 255.0, 0);//(1)
//normalize(outputImageMat, outputImageMat, 1/255);//(2)
circleImage = [OpenCvModel UIImageFromCVMat:outputImageMat];
Note: convertTo & normalize is OpenCv built in Methods.
Where circleImage is UIImage type &
For(1) it is giving me Yellowish image
For (2) it is giving me totally blank image.
The output Image should be very light (nearly black image)
how can i proceed ?,Any Modification, Idea?

How to convert CvMat* to cv::Mat in OpenCV3.0

In opencv2.4.10 which I used before, conversion from CvMat* to cv::Mat can be done as below.
CvMat *src = ...;
cv::Mat dst;
dst = cv::Mat(src);
However, in opencv3.0 rc1 cannot convert like this.
In certain website, this conversion can be done as below.
CvMat* src = ...;
cv::Mat dst;
dst = cv::Mat(src->rows, src->cols, src->type, src->data.*);
If type of src is 'float', the last argument is 'src->data.fl'.
Why constructor of cv::Mat is decreased?
Or are there some methods about conversion from CvMat* to cv::Mat?
CvMat* matrix;
Mat M0 = cvarrToMat(matrix);
OpenCV provided this function instead of Mat(matrix).
Note: In OpenCV 3.0 they wrapped up all the constructors which convert old-style structures (cvmat, IPLImage) to the new-style Mat into this function.
In order to convert CvMat* to Mat you have to do like this:
cv::Mat dst(src->rows, src->cols, CV_64FC1, src->data.fl);

How create new OpenCV Mat object with GCC compiler?

I have gray Mat (image). I wanna create color image the same size as gray image:
With Visual C++ Express it compiled:
Mat dst = cvCreateImage(gray.size(), 8, 3);
but with GCC compiler is error:
threshold.cpp|462|error: conversion from ‘IplImage* {aka _IplImage*}’ to non-scalar type ‘cv::Mat’ requested|
I change to cvCreateMat
Mat dst = cvCreateMat(gray.rows, gray.cols, CV_8UC3);
but GCC still:
threshold.cpp|462|error: conversion from ‘CvMat*’ to non-scalar type ‘cv::Mat’ requested|
Is method create directly Mat or is any conversion?
cvCreateImage(gray.size(), 8, 3);
is from the old , deprecated c-api. don't use it (it's actually creating an IplImage*).
construct a cv::Mat like this:
Mat dst(gray.size(), CV_8UC3); // 3 uchar channels
note, that you will never have to pre-allocate anything for result images,
so, if i.e. you want to do a threshold operation, it's just:
Mat gray = ....;
Mat thresh; // intentionally left empty!
threshold( gray,thresh, 128,255,0);
// .. go on working with thresh. no need to release it either.

C++ and OpenCV: Issue Converting Image to grayscale

Here is my code. It's pretty simple.
Mat image = imread("filename.png");
imshow("image", image);
waitKey();
//Image looks great.
Mat image_gray;
image.convertTo(image_gray, CV_RGB2GRAY);
imshow("image", image_gray);
waitKey();
But when I call the image.convertTo(image_gray, CV_RGB2GRAY); line, I get the following error message:
OpenCV Error: Assertion failed (func != 0) in unknown function, file ..\..\..\sr
c\opencv\modules\core\src\convert.cpp, line 1020
Using OpenCV 2.4.3
The method convertTo does not do color conversion.
If you want to convert from BGR to GRAY you can use the function cvtColor:
Mat image_gray;
cvtColor(image, image_gray, CV_BGR2GRAY);
The function cv::Mat::convertTo is not for color conversion. It is for type conversion. The destination image should have same size and number of channels as the source image.
To convert from RGB to Gray, use the function cv::cvtColor.
cv::cvtColor(image,image_gray,CV_RGB2GRAY);
If you need to acquire video (e.g. from a webcam) in Grayscale, you can also set the saturation of the video feed to zero. (Ex. in Python syntax)
capture = cv.CaptureFromCAM(camera_index)
...
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_SATURATION,0)
image.convertTo(image_gray, CV_RGB2GRAY);
This's wrong.Correct one is,
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
Try this.