Hi I am trying to run a simple OpenCV program to detect contours in a picture. however, findcontours keeps throwing exceptions.
here is my code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
/** #function main */
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <string.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char * argv[]) {
IplImage* img = cvLoadImage("C:\\Users\\310217955\\Documents\\Visual Studio 2010\\Projects\\aviTest\\Debug\\Pictures\\Capture.jpg");
cv::Mat image = cv::cvarrToMat(img);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
//std::vector<std::vector<cv::Point> > contours;
vector<cv::Mat> contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);
system("PAUSE");
return 0;
}
here is an image of the exception:
how can I get around this? is there a dll file im missing or some settings i need to resolve?
I did the following changes to the code
vector<Vec4i> hierarchy;
findContours( contourOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
but now I get the following exception:
Well your code works perfectly fine.I only commented out the header files stdafx.h and nonfree.hpp in my project file . This is the output i gotenter image description here
Make sure you providing the correct path to the input img or paste your input image in your visual studio solution directory and just load the image directly.
The problem may be with one of the modules. I would suggest reinstalling opencv(try downloading the latest version) and try again. Make sure you are adding xyzd.lib files like opencv_highgui330d.lib opencv_imgproc330d.lib
in linker>>Input>> additional dependencies. the version might differ for you.
Hope this helps you!
Related
I'm trying to run the following code and convert the RGB image to YCbCr color model. But when building this code segment it gives the above error. I have attached a screenshot. Can you refer that and give me a solution.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace cv2;
using namespace std;
int main()
{
Mat src1;
src1 = imread("face.jpg", CV_LOAD_IMAGE_COLOR);
namedWindow("Original image", CV_WINDOW_AUTOSIZE);
imshow("Original image", src1);
Mat gray, edge, draw;
//cvtColor(src1, gray, CV_BGR2GRAY);
Mat imgYCC = cv2.cvtColor(src1, cv2.COLOR_BGR2YCR_CB);
//equalizeHist(gray, draw);
//Canny(gray, edge, 50, 255, 3);
edge.convertTo(draw, CV_8U);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", imgYCC);
waitKey(0);
return 0;
}
The namespace cv2 doesn't exists. It's the name of the python wrapper.
Just remove the line:
using namespace cv2;
and don't use it in your code, e.g.:
Mat imgYCC = cvtColor(src1, COLOR_BGR2YCR_CB);
I'm writing program using OpenCV-3.1.0 in Visual Studio 2015. Most of the operations work fine, however, I get an Access Violation error. I have debugged the project, after faceClassifier.load("haarcascade_frontalface_alt.xml") is executed, the Locals windows displays "Information not available, no symbols loaded for opencv_world310.dll". Here's the code:
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
void detectAndDisplay(Mat);
int main()
{
Mat img = imread("faces.jpg");
detectAndDisplay(img);
return 0;
}
void detectAndDisplay(Mat img)
{
CascadeClassifier faceClassifier;
faceClassifier.load("haarcascade_frontalface_alt.xml");
vector<Rect> faces;
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
faceClassifier.detectMultiScale(gray, faces,1.1,3,0);
for (int i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
}
namedWindow("Faces", 1);
while (true)
{
imshow("Faces", img);
if (waitKey(30) >= 0) break;
}
}
You are not using the OpenCV debug libraries. The normal libraries do not contain debug information.
The debug libraries have a d before .dll or .lib
Try
opencv_world310d.dll
I am using OpenCV version 2.4.10.
When I debug I get the breakpoint error: wkernelbase.pdb not loaded.
Furthermore, I get this error in the output of Visual Studio:
First-chance exception at 0x7543C42D in Perspective.exe: Microsoft C++ exception: cv::Exception at memory location 0x003FEDDC.
Unhandled exception at 0x7543C42D in Perspective.exe: Microsoft C++ exception: cv::Exception at memory location 0x003FEDDC.
In my application, the command line prints this out:
OpenCV Error: Assertion failed <0 <= contourIdx< <int>last> in cv::drawContours, file...\..\..\..\opencv\imgproc\src\contours.cpp, line 1810
Any suggestions as to how to deal with this? Here is my code:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
Mat image;
image = imread("shape.jpg", 1);
namedWindow("Display window", CV_WINDOW_AUTOSIZE);
imshow("Display window", image);
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
Canny(gray, gray, 100, 200, 3);
/// Find contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
RNG rng(12345);
findContours(gray, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
/// Draw contours
Mat drawing = Mat::zeros(gray.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
}
imshow("Result window", drawing);
waitKey(0);
return 0;
}
I know my image is in the correct directory as well.
This is a known compatibility issue between vs2013 and opencv
Try to replace
vector<vector<Point> > contours;
with
vector<cv::Mat> coutours;
That works in my case.
I tried your code.
This example (probably) doesn't make sense, but your code works.
This entire thing about VC10, VC12, ... can be quite messy. My suggestion is to try to follow the OpenCV HOWTO for Windows and Visual Studio. Maybe that way you will get around the problems you described in your question (or even find out what's wrong).
i am using backgroundMOG2 to save the images with background , but the program shows the countrs , i need to save the images with backgorund.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <opencv2/video/background_segm.hpp>
int main()
{
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
//cv::Mat img;
cv::VideoCapture cap("C:\\Users\\Pavilion\\Documents\\Visual Studio 2013\\Projects\\dynamic\\sherlock outtakes.avi");
cv::BackgroundSubtractorMOG2 bg;
bg.set("nmixtures", 3);
//bg.bShadowDetecction=false;
std::vector<std::vector<cv::Point>>countours;
//cv::imread("C:\\Users\\Pavilion\\Documents\\Visual Studio 2013\\Projects\\video\\video\\frames\\frame122.jpg");
//cv::VideoCapture Cap;
//std::vector<std::vector<cv::point> >contours;
cv::namedWindow("frame");
cv::namedWindow("background");
for (;;)
{
cap >> frame;
bg.operator()(frame, fore);
bg.getBackgroundImage(back);
cv::erode(fore, fore, cv::Mat());
cv::dilate(fore, fore, cv::Mat());
cv::findContours(fore,countours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::drawContours(frame, countours, -1, cv::Scalar(0, 0, 255), 1);
cv::imshow("frame", back);
// cv::imwrite("background",back);
cv::imshow("background", frame);
if (cv::waitKey(30) >=0) break;
}
return 0;
}
this is the code i am getting error while saving image .
can someone help me to solve this
You need to specify an extension (jpg, png, ...).
For example:
cv::imwrite("background.jpg",back);
I want to capture a video and display it on one window and have second window in which contours are displayed simultaneous. I am struggling with how to have the processed video displayed in the second window. Please analyze my code and suggest a solution or indicate where am going wrong maybe give me some directions to an online tutorial or sources. Thanks.
#include "iostream"
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>
#include <iostream>
#include <vector>
#include <string>
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp> // Video write
using namespace cv;
using namespace std;
Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
Mat frame;
int thresh=100, max_thresh=255;
int main(int argc, char** argv) {
//Capture Video
VideoCapture capCam(1);
if (!capCam.isOpened()){
cout<<"ERROR: Failed to Initialize Camera"<<endl;
return 1;
}
else{
cout<<"Camera Initialized"<<endl;
}
//Create Window
char* ImputFootage = "Source";
namedWindow(ImputFootage, CV_WINDOW_AUTOSIZE);
imshow(ImputFootage, frame);
char* OutputFootage = "Processed";
namedWindow(OutputFootage, CV_WINDOW_AUTOSIZE);
imshow(OutputFootage, frame);
while(1){
capCam>> frame;
imshow("Source", frame);
return(1);
if(capCam.read(ImputFootage)){
//Convert Image to gray & blur it
cvtColor( image,
image_gray,
CV_BGR2GRAY );
blur( image_gray,
image_gray2,
Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2,
threshold_output,
thresh,
max_thresh,
THRESH_BINARY);
//2D Container
vector<vector<Point>> contours;
//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
contours, // a vector of contours
CV_RETR_EXTERNAL,// retrieve the external contours
CV_CHAIN_APPROX_NONE,
Point(0, 0)); // all pixels of each contours
// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));
drawContours(result,contours,
-1, // draw all contours
Scalar(0), // in black
2); // with a thickness of 2
}
}
char CheckForEscKey = waitKey(10);
return 1;
}
You should call imshow("Processed", result); after calling drawContours
You were trying to show frames even before they were captured with camera. Compiler was not giving you error because Mat were declared ,but they were without value (null), Moreover you were trying to display Mat image, but what you capture from camera is Mat frame. Also, you lack exit (esc sequence, and your wait key was OUT of camera loop.
Anyway, here is your code (rewritten), I hope this is what you wanted.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
using namespace cv;
Mat image;
Mat image_gray;
Mat image_gray2;
Mat threshold_output;
Mat frame;
int thresh = 100, max_thresh = 255;
int main(int argc, char** argv)
{
//Capture Video
VideoCapture capCam(0);
if (!capCam.isOpened())
{
cout << "ERROR: Failed to Initialize Camera" << endl;
return 1;
}
else
{
cout << "Camera Initialized" << endl;
}
//Create Window
char* ImputFootage = "Source";
namedWindow(ImputFootage, CV_WINDOW_AUTOSIZE);
char* OutputFootage = "Processed";
namedWindow(OutputFootage, CV_WINDOW_AUTOSIZE);
while (1)
{
capCam >> frame;
imshow(ImputFootage, frame);
if (capCam.read(frame))
{
//Convert Image to gray & blur it
cvtColor(frame, image_gray, CV_BGR2GRAY);
blur(image_gray, image_gray2, Size(3, 3));
//Threshold Gray&Blur Image
threshold(image_gray2, threshold_output, thresh, max_thresh, THRESH_BINARY);
//2D Container
vector<vector<Point> > contours;
//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output, contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE, Point(0, 0)); // all pixels of each contours
// Draw black contours on a white image
Mat result(threshold_output.size(), CV_8U, Scalar(255));
drawContours(result, contours, -1, // draw all contours
Scalar(0), // in black
2); // with a thickness of 2
imshow(OutputFootage, result);
char CheckForEscKey = waitKey(10);
//If the key pressed by user is Esc(ASCII is 27) then break out of the loop
if (CheckForEscKey == 27)
{
break;
}
}
}
return 0;
}