Cant save image using OpenCV - c++

I was trying to save images from a video file but It couldn't save any image in my hard drive. I compiled following program without errors.
#include <stdio.h>
#include <stdlib.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main(int argc, char* argv[]) {
int c=1;
IplImage* img=0;
IplImage* small;
IplImage* small=new IplImage(inputArray);
char buffer[1000];
CvCapture* cv_cap=cvCaptureFromFile("test.avi");
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
small = cvCreateImage(cvSize(img->width/2,img->height/2), 8, 3);
sprintf(buffer,"C:/image%u.jpg",c);
c++;
if (cvWaitKey(100)== 27) break;
}
cvDestroyWindow("Video");
return 0;
}
I am using a 2.3.1 openCV library and Visual Studio 2010.
How can I save the image from a video file?
I have also tried this code,above program code are something wrong.please see following program code.After i adding following code ,also there was a same problem.
cvSaveImage(buffer,img);
Corrected program is shown below.please see this and tell me what i did wrong.but there is not compile error.after i run the program it doesn't save any image.
#include"stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<cxcore.h>
int main(int argc, char* argv[]) {
int c=1;
IplImage* img=0;
char buffer[1000];
CvCapture* cv_cap=cvCaptureFromCAM(-1);
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
sprintf(buffer,"D:/image%u.jpg",c);
cvSaveImage(buffer,img);
c++;
if (cvWaitKey(100)== 27) break;
}
cvDestroyWindow("Video");
return 0;
}
can you tell me how to save a image .above program doesn't save any images.please give me your suggestions.thank you.

You forgot to actually save the image:
cvSaveImage(buffer ,img);
Also, the following is redundant:
IplImage* small; // <-- you don't need this
IplImage* small=new IplImage(inputArray);

Don't know if you solved the problem or not. if not you can use imwrite("test.jpg",img); to save an image,
here is an example-
while(1) {
img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
imwrite("test.jpg",img);
}

Related

Done building project "Project.vcxproj" -- FAILED

I wrote some lines of code to simply read and write an image and it was working a few days ago, but now Visual Studio gives these errors: "1>Done building project "Lab1_PI.vcxproj" -- FAILED." and it says it was "unable to start the program" and that the system cannot find the file specified.
I also tried to add add _CRT_SECURE_NO_DEPRECATE and _CRT_SECURE_NO_WARNINGS via right-click project->C/C+±>Preprocessor->Preprocessor Definitions, but nothing changed.
Also, here's my code, but it is correct since it was working a few days ago:
#include <iostream>
#include <string>
#include "opencv2\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main(int arc, const char** argv) {
string imgPath = argv[1];
string img2Path = argv[2];
cv::Mat img = imread(imgPath);
if (!img.data) {
cout << "error:no image" << endl;
return -1;
}
imshow("flower image (color)", img);
Mat img2;
cvtColor(img, img2, COLOR_RGB2GRAY);
imwrite(img2Path, img2);
imshow("flower image (grey)", img2);
waitKey(0);
}
I would be really grateful if someone could help me fix this problem because it stops me from using Visual Studio and I really need it for a project!

Suddenly cannot create new openCV applications, windows 10

I have been using openCv for a while, but have just moved to windows 10.
Now, existing applications will compile, but I cannot make a new one.
In a new project (visual studio 2015, release 64)
I am adding all the libs:
opencv_calib3d310.lib
opencv_core310.lib
opencv_features2d310.lib
opencv_flann310.lib
opencv_highgui310.lib
opencv_imgcodecs310.lib
opencv_imgproc310.lib
opencv_ml310.lib
opencv_objdetect310.lib
opencv_photo310.lib
opencv_shape310.lib
opencv_stitching310.lib
opencv_superres310.lib
opencv_ts310.lib
opencv_video310.lib
opencv_videoio310.lib
opencv_videostab310.lib
setting:
D:\opencv-master\build64\lib\Release;%(AdditionalLibraryDirectories)
and
D:\opencv-master\modules\highgui\include
D:\opencv-master\modules\imgcodecs\include
D:\opencv-master\modules\core\include
D:\opencv-master\modules\videoio\include
D:\opencv-master\modules\imgproc\include
%(AdditionalIncludeDirectories)
and adding the most basic:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "stdafx.h"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window", image); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
In visual studio, Intellisense is fine, no red underlines, everything looks good. When I try to compile tho...
'cv': a namespace with this name does not exist
'Mat': undeclared identifier
'image': undeclared identifier
...and many more. It is like it cannot find the libs, but i am linking them correctly, I am sure of it.
Can anyone assist me here?

VideoCapture not working C++ windows

So I have played around in OpenCV a bunch before and never run into this problem. I am implementing a MeanShift algorithm and trying to do it on video devices, images, and videos. Devices and images work; however, no matter what I try, when I run VideoCapture on my filename (whether setting it in the Constructor or using the VideoCapture::open() method, and whether local or with a full path) I always get stuck in my error check.
Thoughts? Ideas? code below. running in Visual Studio 2012
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\video\video.hpp"
#include <string>
using cv::Mat;
using std::string;
enum Filetype{Image, Video};
int main(int argc, char* argv[])
{
string filename = "short_front.avi";// "C:\\Users\\Jonathan\\Videos\\short_front.mp4"; //"hallways.jpg";
Mat cv_image; //convert to unsigned char * with data
Mat filtImage_;
Mat segmImage_;
Mat whiteImage_;
cv::VideoCapture vid;
vid.open("C:/Users/Jonathan/Desktop/TestMeanShift/TestMeanShift/short_front.avi");
cv::waitKey(1000);
if ( !vid.isOpened() ){
throw "Error when reading vid";
cv::waitKey(0);
return -1;
}
// cv_image = cv::imread(filename);//, CV_LOAD_IMAGE_COLOR);
// if(! cv_image.data){
// std::cerr << "Image Failure: " << std::endl;
// system("pause");
// return -1;
// }
//Mat cv_image_gray;
//cv::cvtColor(cv_image,cv_image_gray,CV_RGB2GRAY);
for (;;)
{
vid >> cv_image;
if ( !cv_image.data)
continue;
cv::imshow("Input",cv_image); //add a normal window here to resizable
}
EDIT: This is a distinct problem from the one listed here because it deals with a specific corner case: VideoCapture and ImageCapture both work, only not VideoCapture with a file. When it doesn't work, the code runs properly, except that the "video" it creates is incomplete as it didn't open properly. Therefore, as the code above does not crash in compile time or run time, the only indicator is bad output (6KB video output file). If you are having issues not with the corner case I am describing but general issues with the above functions in OpenCV, the aforementioned link could help you.

ERROR: identifier "img" is undefined

I'm beginner to openCV. I dowloaded opencv2.4.5 and visual studio express 2012 then i followed this link http://opencv-srf.blogspot.in/2013/05/installing-configuring-opencv-with-vs.html for setup everything in environment variable etc. Then i followed below link http://opencv-srf.blogspot.in/2013/06/load-display-image.html to create sample application. I included proper #include path. But i'm getting error.
#include "stdafx.h"
#include <C:\opencv\build\include\opencv\cv.h>
#include <C:\opencv\build\include\opencv\cxcore.h>
#include <C:\opencv\build\include\opencv\highgui.h>
#include "C:\opencv\build\include\opencv2\highgui\highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
please do not use an absolute path for the includes, this is totally non-portable.
it should look like this:
// the usual suspects:
#include "opencv2\core\core.hpp" // Mat is defined here.
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
also, to make this work, your "additional include folders" should point to "opencv/build/include"
and avoid the old c-api headers, like cv.h, highgui.h, cxcore.h

OpenCV Error: Assertion failed in cvAdaptiveThreshold

I recently started some OpenCV programming on OSX (just using text editor and compiling in terminal). I found program on the internet that is very useful to me but can't seem to run it.
This is the code:
#include <stdio.h>
#include "cv.h"
#include <highgui.h>
#include <iostream>
#include <cstdio>
using namespace std;
int widthU;
int heightU;
int xU = 0;
int yU = 0;
int main(int argc, char *argv[])
{
IplImage *imgPicThres, *imgPicInput;
imgPicInput = cvLoadImage("bitmap.png", -1);
imgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);
cvNamedWindow("Input picture", 0);
cvNamedWindow("Thres picture", 0);
//Picture
//cvThreshold(imgPicInput,imgPicThres,100,255,CV_THRESH_BINARY);
cvAdaptiveThreshold(imgPicInput, imgPicThres,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
cvShowImage("Input picture", imgPicInput);
cvShowImage("Thres picture", imgPicThres);
while (true)
{
int c = cvWaitKey(10);
if(c==27)
break;
}
cvDestroyWindow("Input picture");
cvDestroyWindow("Thres picture");
return 0;
}
And this is the error that I get:
OpenCV Error: Assertion failed (src.size == dst.size && src.type() == dst.type()) in cvAdaptiveThreshold, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.5/modules/imgproc/src/thresh.cpp, line 873
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6
I tried to change this line
ImgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);
into
ImgPicThres = cvCreateImage(cvGetSize(imgPicInput), IPL_DEPTH_8U, 1);
with no luck.
OpenCV is installed via Macports and is running the latest version. Any help would be appreciated. Thanks!
imgPicInput = cvLoadImage("bitmap.png",CV_LOAD_IMAGE_GRAYSCALE);
to ensure that the image you read is actually grayscale.
Additionally to suggestion from perfanoff, I'd rather clone image rather then creating it.
imgPicThres = cvCloneImage(imgPicInput );
I have found the answer but forgot to mention it. As the error says imgPicInput and imgPicThres are not of the same size and type. Also I was supposed to watch after the image channels which I didn't.