Imshow from opencv is not working anymore - c++

I wrote some simple programs using imshow function. It worked fine few times. I've tried to view two pictures, before and after processing. For the first time it worked fine, but the second time it crashed my application.
Now imshow crash application every time.
How to fix that?
#include <iostream>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat image, gray_image;
string file_path;
cout << "Input file path: ";
cin >> file_path;
image = imread(file_path, CV_LOAD_IMAGE_UNCHANGED);
if (image.data==NULL)
{
cout << "No image found!";
return 1;
}
cvtColor(image, gray_image, CV_BGR2GRAY);
namedWindow("Orig", CV_WINDOW_AUTOSIZE);
namedWindow("Gray", CV_WINDOW_AUTOSIZE);
imshow("Orig", image);
imshow("Gray", gray_image);
cout << "Output file path: ";
cin >> file_path;
imwrite(file_path, gray_image);
return 0;
}

You need waitKey
See what does waitKey (30) mean in OpenCV?

Related

Cannot read video from file while can read from camera

My problem is that I cannot read from file I tried with .mp4 and .mov using cpp code on windows with library opencv3.4.0. I tried to read from camera it is working.
What could be the reason ?
cv::VideoCapture cap("001.mp4");
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break; //==>hits here
}
/// while below code part works correctly
cv::VideoCapture cap(0);
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break;
}
Copy and try below code, if it doesnt work either. It is completely about your opencv installation or directory mistake.
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
VideoCapture cap("videoname.mp4");
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow( "Frame", frame );
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}

Failed to output video file by OpenCV on MacOS with C++

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
VideoCapture vcap("0");
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
int frame_width= vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("~/out.avi", CV_FOURCC('J','P','E','G'), 10, Size(frame_width,frame_height),true);
for(;;){
Mat frame;
vcap >> frame;
video << frame;
imshow( "Frame", frame );
char c = (char)waitKey(33);
if( c == 'q' ) break;
}
return 0;
}
I have tried the above code, the program can correctly show the camera image through imshow(), but after terminate the program, no video output file has written to the disk. I use MacOS Xcode and OpenCV 2.4.13 as development platform. The same work and output the video file completely on Windows Visual Studio. Please help.

Xcode7.3.1-Why the image can not be found and ended with exit code:255

#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
int main()
{
char filename[] = "IMG_0851";
Mat image; //create an image matrix
I want to analysis the image in grayscale
image = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE); //load image in grayscale directly
if (image.empty())
{
cout << "Image could not be found" << endl;
return -1;
}
imshow("Image in RGB", image);
cvWaitKey(0); //wait for a keystroke in the window
return 0;
}
Build succeeded, however, the image cannot be found & program ended exit code: 255.

Taking multiple pictures in opencv

This seems fairly straightforward, but for the life of me, I cannot figure out why this will not work. I have similar code that writes an image everytime it reads an image, and that works fine saving the last image seen. I am seriously puzzled as to why this is saving the same image to both img0 and img1. If you guys could shed some light, that would be amazing! Thanks so much for taking the time to read this.
#include "highgui.hpp"
#include "imgproc.hpp"
#include "opencv.hpp"
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
using namespace cv;
int main(){
VideoCapture stream(0);
if(!stream.isOpened()){
cout << "No camera :(\n";
}
stream.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int img_num = 0;
int num_pics;
cout << "How many images do you want to take?\n";
cin >> num_pics;
Mat image;
while(img_num < num_pics){
cout << "Picture in...\n";
cout << "3...\n";
sleep(1);
cout << "2...\n";
sleep(1);
cout << "1...\n";
sleep(1);
stream.read(image);
imshow("pic",image);
imwrite(format("img_%d.jpg",img_num),image);
waitKey(3000);
img_num += 1;
}
return 0;
}
Edit to add simple code for saving every frame captured (into the same file, so should ultimately be the last image seen):
#include "/home/sarah/opencv-2.4.9/modules/highgui/include/opencv2/highgui/highgui.hpp"
#include "/home/sarah/opencv-2.4.9/modules/imgproc/include/opencv2/imgproc/imgproc.hpp"
#include "/home/sarah/opencv-2.4.9/include/opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
VideoCapture stream(0);
//stream.set(CV_CAP_PROP_FPS,1);
if(!stream.isOpened()){
cout << "No camera :(\n";
}
cout << "After\n";
stream.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
Mat cameraFrame;
while(1){
stream.read(cameraFrame);
imshow("camera",cameraFrame);
imwrite("img.jpg",cameraFrame);
if(waitKey(30) == 13){
break;
}
}
return 0;
}
here's the culprit:
imwrite(filename,image);
atm, it will save any image to the same filename(thus overwriting any previos ones). what you want is probably more similar to:
imwrite( format("img_%d.jpg",img_num) ,image );

Displaying Images from file using OpenCV VideoCapture, and C++ vectors

I am reading in an image sequence from a file using an OpenCV VidoeCapture - I believe I am doing this part correctly - and then putting them in a c++ vector, for processing at a later point.
To test this, I wrote the following that would read in images, put them in a vector, and then display those images from the vector one by one. However, when I run this, no images appear.
What's wrong?
I am using a raspberry pi, I don't know if that makes any difference.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
vector<Mat> imageQueue;
int main(int argc, char** argv)
{
string arg = ("/home/pi/pictures/ceilingSequence/%02d.jpg");
VidoeCapture sequence(arg);
if(!sequence.isOpened()) {
cout << "Failed to open image sequence" << endl;
return -1;
}
Mat image;
for(;;)
{
sequence >> image;
if(image.empty()) {
cout << "End of sequence" << endl;
break;
}
imageQueue.push_back(image);
}
for(int i = 0; i < 10; i++)
{
//display 10 images
Mat readImage;
readImage = imageQueue[i];
namedWindow("Current Image", CV_WINDOW_AUTOSIZE);
imshow("Current Image", readImage);
sleep(2);
}
return 0;
}
please replace the sleep(2) with a waitKey(2000). // assuming you want to wait for 2 seconds
even if you're not interested in keypresses in general, it is needed to update the opencv / highgui graphics loop correctly.