opencv c++ HSV image channel separation exception - opencv

I know this question has been asked a number of times and I'm trying to implement their answers but its causing an exception in my code.
OS: Windows 7
OpenCV: 2.4.9
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from webcam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
Mat imgHSV;
_sleep(100); //give the camera a chance to wake up
while (true)
{
Mat imgOriginal;
bool success = cap.read(imgOriginal);
if (!success) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
vector<Mat> hsv_planes;
split( imgHSV, hsv_planes );
//hsv_planes[0] // H channel
//hsv_planes[1] // S channel
//hsv_planes[2] // V channel
imshow(thresholded_window, hsg_planes[2]); //show the S channel
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Exception is thrown on the split line: "Unhandled exception at 0x62B978C9 (opencv_core249.dll) in TrackColour.exe: 0xC0000005: Access violation writing location 0xFEEEFEEE."

I figured out my problem, incase this happens to anyone else.
In VS I was doing a debug build but I was linking to the non debug versions of opencv. Notice in my error message opencv_core249, it should have been opencv_core249d (Note the 'd'). I updated my CV linking to use the debug libraries and it works.
The other opencv calls performed fine using the wrong libraries, something must be unique with split.

Related

Image panorama stitching black spots

Maybe someone knows what can cause that problem.
I have a simple code for stitching 12 images in one panorama image, but faced with strange behaviour.
Here the result of stitching on my MacBook Pro:
https://drive.google.com/open?id=1f_7i_rb8pnbHEBxlowzFa13MAHvxN07u
And result of stitching on my Desktop Mac:
https://drive.google.com/open?id=19CoyPzz6QgDjqG1lUZbphGPYvCAg41hi
The code i used is the same and images same too. On my desktop mac i can build and run it without problems, but on my MacBook it not working at all, it shows me the next error:
OpenCV Error: Assertion failed (imgs.size() == imgs_.size())
in composePanorama, file /tmp/opencv-20171031-87373-6u1izq/opencv-3.3.1/modules/stitching/src/stitcher.cpp, line 168
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-20171031-87373-6u1izq/opencv-3.3.1/modules/stitching/src/stitcher.cpp:168:
error: (-215) imgs.size() == imgs_.size() in function composePanorama
This is my code:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv ) {
vector<Mat> images_arr;
const string images[] = {
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/1.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/2.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/3.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/4.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/5.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/6.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/7.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/8.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/9.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/10.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/11.jpeg",
"/Users/george/CLionProjects/OpenCV_Stitch/test_stitch/12.jpeg",
};
const char* outFile = "/Users/george/CLionProjects/OpenCV_Stitch/out.jpeg";
for(auto path : images) {
Mat image = imread(path, IMREAD_REDUCED_COLOR_2 | IMREAD_IGNORE_ORIENTATION);
images_arr.push_back(image);
}
Mat pano;
Stitcher stitcher = Stitcher::createDefault(false);
stitcher.setRegistrationResol(0.8);
stitcher.setSeamEstimationResol(0.1);
stitcher.setCompositingResol(1);
stitcher.setPanoConfidenceThresh(1);
stitcher.setWaveCorrection(true);
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
Stitcher::Status status1 = stitcher.estimateTransform(images_arr);
Stitcher::Status status = stitcher.composePanorama(images_arr, pano);
if (status != Stitcher::OK) {
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
imwrite(outFile, pano);
return 0;
}
My CmakeLists.txt configuration:
cmake_minimum_required(VERSION 3.8)
project(OpenCV_Stitch)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(OpenCV_Stitch ${SOURCE_FILES})
target_link_libraries(OpenCV_Stitch ${OpenCV_LIBS})
Can't even imagine what can cause that black spots on image. Did anyone come across this? And sometimes, when i use different images to stitch opencv returns error: Camera parameters adjusting failed
How can i prevent that? Maybe use some methods to estimate camera rotation or something like that?
Here the link to images what i try to stitch:
https://drive.google.com/open?id=1-DfSf8eaC7bi37-ZhBpaRt8jHVvO_Qwb
Thanks!
i get the same error message with your code. when i modified the part below it runs without error.
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(Stitcher::PANORAMA, false);
stitcher->setRegistrationResol(0.8);
stitcher->setSeamEstimationResol(0.1);
stitcher->setCompositingResol(1);
stitcher->setPanoConfidenceThresh(1);
stitcher->setWaveCorrection(true);
stitcher->setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
Stitcher::Status status = stitcher->stitch(images_arr, pano);

OpenCv 3.1.0 undefined reference to imread

This is what my code looks like, I got the undefined reference to imread and so on:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, const char** argv )
{
Mat img = imread("MyPicture.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;
}
I am using Codeblocks and the g++ compiler. Also I have linked the opencv_world310d.lib to debug and opencv_world310.lib to release.
Plus I have given the path in searchdirectory compiler and linker.
Any hints?

How to operate an PS eye with openCV

I would like to use the external PS eye cam to save 30 frames a second if that is possible.
I don't know where to find guides or the code it self since I'm pretty sure it should be somewhere online.
Anyhelp would be appreciated. Thanks in advance!
So, getting the ps3eye up and running differs depending on which OS you're running. If you're running any flavor of Debian, the drivers are already there and the code I've got below should work fine.
If you're on Windows, you'll have to find a driver for it. CodeLibrary has a driver already made, but you'll have to pay $3 for it. Link's here.
I don't know for Mac, but a bit of digging found this, which might work.
Once you've got the drivers installed, you should be able to access it like any other camera.
Simple bit of code for that is below:
#include "stdafx.h"
#include <opencv/cxcore.h>
#include <opencv2\core\mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/videoio/videoio.hpp>
using namespace cv;
using namespace std;
int main() {
Mat image;
bool escnotpressed = true;
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
cap.set(CV_CAP_PROP_FPS, 30); //sets framerate
String capturePath = "C:/this/is/a/path.avi";
Size frameSize = Size(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter savedCapture;
savedCapture.open(capturePath, VideoWriter::fourcc('M','J','P','G'), 30.0, frameSize, true);
if (!savedCapture.isOpened()) {
return -2;
}
while(escnotpressed) { //loops
cap >> image;
if (image.empty()) {
cout << "camera feed got interrupted" << endl;
return 5; //dies if camera feed is interrupted for some reason
}
imshow("Image", image);
savedCapture << image;
int c = waitKey(10);
if( (char)c == 27 ) { escnotpressed = false;}
}
savedCapture.release();
cout << "Done!" << endl;
}
EDIT: If you've got multiple webcams on your computer, you might need to change that VideoCapture cap(0) to VideoCapture cap(x), where x gives you the right camera.

After reading multiple frames from a camera, OpenCV suddenly always fails to read frames. How do I diagnose this?

I run a program similar to the one in this question: https://stackoverflow.com/a/8719192/26070
#include <opencv/highgui.h>
#include <iostream>
/** #function main */
int main( int argc, char** argv )
{
cv::VideoCapture vcap;
cv::Mat image;
const std::string videoStreamAddress = "rtsp://192.0.0.1:8081/live.sdp";
//open the video stream and make sure it's opened
if(!vcap.open(videoStreamAddress)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey(500);
} else {
cv::imshow("Output Window", image);
}
if(cv::waitKey(1) >= 0) break;
}
}
The program runs fine for a certain amount of time (about one minute or so) and then the call to read() (method from cv::VideoCapture) always returns false.
The output is as follows:
[mpeg4 # 00da27a0] ac-tex damaged at 22 7
[mpeg4 # 00da27a0] Error at MB: 309
No frame
No frame
No frame
Note: the first two lines are not always present.
So, how can I determine what the root of the problem is?

MJPEG network stream to OpenCV 2

Can anyone explain to me why this code below does not work?
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <iostream>
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
const std::string videoStreamAddress = "http://hg55.no-ip.org/mjpg/video.mjpg";
//Yes, this stream does work! Try to paste it into your browser...
//open the video stream and make sure it's opened
if(!vcap.open(videoStreamAddress)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if(cv::waitKey(1) >= 0) break;
}
}
This code does cannot open the stream...
The code is based on some code in this thread: OpenCV with Network Cameras
The OpenCV 1 code here works without any problem for me.
Thank you very much in advance
I tried to create a new project with Visual Studio 2010 and OpenCV 2.2, instead of OpenCV 2.3.1.
This solved all my problems!

Resources