cvLoadImage works with string constant but not c_str() - opencv

I am using cvloadimage to open an image in my program. The images are in a external directory, generated using the current time in the filename.
When I try to load images in this way:
IplImage *image = cvLoadImage(path.c_str,CV_LOAD_IMAGE_COLOR);
the image structure will be NULL and the application will stop with q segmentation fault.
When I try to load an image this way:
IplImage *image = cvLoadImage("path/images/image_2012_11_25.jpg",CV_LOAD_IMAGE_COLOR);
then it works great.
Is the problem that cvLoadImage can't accept any type of text, only const char*?
But c_str() converts string to char*, right?
How can I solve this problem?

I had the same problem with writing into a file so I used the code below:
const char *M=s.c_str();
file.write(M,s.size());
hope it works...

Related

How can i convert a const mat image into an Iplimage?

i'm working on the extraction of an algorithm from a source existing library. Inside a function there is a declaration of IplImage initialized with a Const cv::mat :
IplImage *frame = new IplImage(img_input);
compiling i get the following error:
no matching function for call to ‘_IplImage::_IplImage(const cv::Mat&)
I think this is because the library uses an old version of opencv. So the question is how can i get the same result in the new version of opencv?(i'm using the latest)

What does the command String imageName( "../data/HappyFish.jpg" ); do?

I am a total beginner in OpenCV. I was learning a program to load and display an image where the below statement is used.
String imageName( "../data/HappyFish.jpg" ); //by default
what does this statement do?
This statement is the folder directory of the image file HappyFish.jpg in the OpenCV example Load and Display an Image. Simply it is used with imread to indicate the location where our image file is located.
image = imread( imageName, IMREAD_COLOR ); // Read the file

loading image in opencv2.4.6

In opencv 2.4.6. I am trying to load a mat image file with a simple code given below. But the image is not loaded as I print the image size, it is showing '0'. Can anybody please tell me , what is going wrong?
int main(int argc, char argv[])
{
Mat a=imread("C:/image3.jpg");
cv::Size frame11_size = a.size();
printf("%d",frame11_size.height);
return 0;
}
Update: I solved the problem. The problem was, I was only including all the library,include and additional dependencies in 'debug mode' only. I did not change anything in 'release mode'. When I change the properties in 'release mode' as-well, it worked. thanks all for your kind responses, I am giving '+1' for your answers.
I think there should be single slash on your image path, and always check whether image is successfully loaded.
Mat a=imread("C:/image3.jpg");
if(! a.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
OpenCV can't open jpg files by itself. It depends on third parties to do so. Maybe you are missing certain dlls, or maybe your OpenCV installation don't have the right path to them. To test this assumption store your image in other formats. For example pgm or ppm. Those formats does not perform any encoding and just store image buffer in file as is. As a result OpenCV will not need any external libraries to open image in ppm format.

Opencv - create png image

As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming.
Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.
The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project.
Firstly, some code:
websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols * websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;
but I am getting strange error during execution of the second line:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
and I am a bit confused why I am getting this error from my code.
Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMatto get all of the png encoded data?
The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. You are passing 0, which is the same thing as NULL. That's why you are getting the message NULL not valid.
You should probably use this:
websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);
You can check out the documentation of cvEncodeImage here.
You can check out some examples of cvEncodeImage, or its C++ brother imencode here: encode_decode_test.cpp. They also show some parameters you can pass to cvEncodeImage in case you want to adjust them.

JavaCV Stitching

I am trying to stitch multiple images by using JavaCV 0.1 and OpenCV 2.4.0 in Java, i use this code for stitching images :
stitcher = Stitcher.createDefault(false);
MatVector images = new MatVector(imageN.size());
for(...){
CvArr image = cvLoadImage(imageN);
images.put(index,image);
}
MatVector result = new MatVector(1);
int status = stitcher.stitch(images,result);
if( status == stitcher.OK )
{
cvSaveImage(result.getIplImage(0));
}
NOTE 1 : Loaded images in this example are valid image for stitching.
NOTE 2 : C++ version of the code runs with no problem on current configuration
In stitcher.stitch method opencv throws an assertion exception such as "k == MAT". How should i fix this? Is MatVector usage is right in this sample code?
Thanks...
I found it, it is a bug related with JavaCv.
Actually JavaCv is not guilty.OpenCV stitcher API uses cv::OutputArray for returning stitched image but this method casts cv::OutputArray to cv::Mat when executing. JavaCV ports OpenCV method only by using parameter interface and so it converts the parameter as std::vector, this results as a assertion failure.
It is required to convert std::vector to Mat to make it working. I don't know any other way exist for this conversion but otherwise it is possible to be fixed by only lib's author.
It is said that c++ version is working but in fact, it is working when pano parameter is given as cv::Mat, when std::vector is entered it gives the same failure assertions again.

Resources