name must be a namespace name Visual Studio Error - c++

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);

Related

OpenCV C++ VS2010: FindContours throws exception

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!

How to solve the fopen error when use opencv

I have a simple code,but it confuse me one day
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
void example2_4(IplImage* image)
{
cvNamedWindow("Example2_4-in", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Example2_4-out", CV_WINDOW_AUTOSIZE);
cvShowImage("Example2_4-in", image);
IplImage* out = cvCreateImage(
cvGetSize(image),
IPL_DEPTH_8U,
3
);
cvSmooth(image, out, CV_GAUSSIAN, 5, 5);
cvSmooth(out, out, CV_GAUSSIAN, 5, 5);
cvShowImage("Example2_4-out", out);
cvReleaseImage(&out);
cvWaitKey(0);
cvDestroyWindow("Example2_4-in");
cvDestroyWindow("Example2_4-out");
}
int main()
{
IplImage* img = cvLoadImage("lena.png");//This is my current image,you can use yourself's to test
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
example2_4(img);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
It always give a error information like the follow image
Could I know which sentence call the "logger.h" file and result that
error?
How to avoid such error?

opencv stereo vision depth map, code does not work

I am studying on stereo vision depth map and I am using the opencv library.I wrote a program to obtain depth map. But when program was run I obtained an empty depth map frame.can anybody help me please, what is wrong ? code are shown in below;
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/contrib/contrib.hpp>
int main()
{
IplImage* img1 = cvLoadImage("/home/sezen/Masaüstü/imR.png");
IplImage* img2 = cvLoadImage("/home/sezen/Masaüstü/imL.png");
IplImage *rimage = cvCreateImage(
cvSize( img1->width, img1->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img1, rimage, CV_RGB2GRAY );
IplImage *limage = cvCreateImage(
cvSize( img2->width, img2->height ), IPL_DEPTH_8U, 1 );
cvCvtColor( img2, limage, CV_RGB2GRAY );
cvNamedWindow( "Right", CV_WINDOW_AUTOSIZE );
cvShowImage( "Right", rimage );
cvNamedWindow( "Left", CV_WINDOW_AUTOSIZE );
cvShowImage("Left", limage);
CvMat *matr = cvCreateMat(rimage->height,rimage->width,CV_8UC1 );
CvMat *matl = cvCreateMat(limage->height,limage->width,CV_8UC1 );
CvMat* disp = cvCreateMat(rimage->height,rimage->width,CV_16S);
CvMat* vdisp = cvCreateMat(rimage->height,rimage->width,CV_16S);
cvConvert( rimage, matr );
cvConvert( limage, matl );
CvStereoBMState *BMState = cvCreateStereoBMState();
assert(BMState != 0);
BMState->preFilterSize=21;
BMState->preFilterCap=31;
BMState->SADWindowSize=21;
BMState->minDisparity=0;
BMState->numberOfDisparities=128;
BMState->textureThreshold=10;
BMState->uniquenessRatio=15;
cvFindStereoCorrespondenceBM( matr, matl, disp, BMState);
cvNormalize(disp, vdisp, 0, 255, CV_MINMAX);
cvShowImage("depthmap", vdisp);
cvWaitKey(0);
return 0;
}
Here's a code for disparity map using C++ API. Final image that you normalize should be of type CV_8UC1.
Mat img1, img2, g1, g2;
Mat disp, disp8;
img1 = imread("leftImage.jpg");
img2 = imread("rightImage.jpg");
cvtColor(img1, g1, CV_BGR2GRAY);
cvtColor(img2, g2, CV_BGR2GRAY);
StereoBM sbm;
sbm.state->SADWindowSize = 9;
sbm.state->numberOfDisparities = 112;
sbm.state->preFilterSize = 5;
sbm.state->preFilterCap = 61;
sbm.state->minDisparity = -39;
sbm.state->textureThreshold = 507;
sbm.state->uniquenessRatio = 0;
sbm.state->speckleWindowSize = 0;
sbm.state->speckleRange = 8;
sbm.state->disp12MaxDiff = 1;
sbm(g1, g2, disp);
normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);
imshow("left", img1);
imshow("right", img2);
imshow("disp", disp8);
I can only add that structure of OpenCV namespaces and classes changes every year.
I placed below working source code for OpenCV 3.4.0
#include <Windows.h>
#include <Vfw.h>
#include <string>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\imgcodecs\imgcodecs.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\calib3d\calib3d.hpp"
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat im_left=imread("right.png");
Mat im_right=imread("left.png");
cv::Size imagesize = im_left.size();
cv::Mat disparity_left=cv::Mat(imagesize.height,imagesize.width,CV_16S);
cv::Mat disparity_right=cv::Mat(imagesize.height,imagesize.width,CV_16S);
cv::Mat g1,g2,disp,disp8;
cv::cvtColor(im_left,g1,cv::COLOR_BGR2GRAY);
cv::cvtColor(im_right,g2,cv::COLOR_BGR2GRAY);
cv::Ptr<cv::StereoBM> sbm = cv::StereoBM::create(0,21);
sbm->setDisp12MaxDiff(1);
sbm->setSpeckleRange(8);
sbm->setSpeckleWindowSize(9);
sbm->setUniquenessRatio(0);
sbm->setTextureThreshold(507);
sbm->setMinDisparity(-39);
sbm->setPreFilterCap(61);
sbm->setPreFilterSize(5);
sbm->compute(g1,g2,disparity_left);
normalize(disparity_left, disp8, 0, 255, CV_MINMAX, CV_8U);
cv::namedWindow("Left",CV_WINDOW_FREERATIO);
cv::imshow("Left", im_left);
cv::namedWindow("Right",CV_WINDOW_FREERATIO);
cv::imshow("Right", im_right);
cv::namedWindow("Depth map",CV_WINDOW_FREERATIO);
cv::imshow("Depth map", disp8);
cv::waitKey(0);
return 0;
}
For me it worked slightly different with the init of the stereoBM object
Ptr<StereoBM> sbm = cv::StereoBM::create(16, 5);
sbm->setDisp12MaxDiff(1);
sbm->setSpeckleRange(8);
sbm->setSpeckleWindowSize(0);
sbm->setUniquenessRatio(0);
sbm->setTextureThreshold(507);
sbm->setMinDisparity(-39);
sbm->setPreFilterCap(61);
sbm->setPreFilterSize(5);
sbm->compute(src1, src2, disp);
I edited the code to my needs, I added the camera one and two, and reading from them.
Then, made the Depth map. Thanks hope its helpful.
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
using namespace std;
using namespace cv;
int main()
{
VideoCapture leftCam(0); //lets say 0 is left, 1 is right
if (leftCam.isOpened() == false){cout << "error: Webcam connect unsuccessful\n"; return(0); }
VideoCapture rightCam(1); //lets say 0 is left, 1 is right
if (rightCam.isOpened() == false){cout << "error: Webcam connect unsuccessful\n"; return(0); }
Mat left, right;
Mat leftClone, rightClone;
char charCheckForEscKey = 0;
while ( charCheckForEscKey != 27 && leftCam.isOpened() )
{
leftCam.read(left);
if (left.empty()){cout << "No frame to read" << endl; break;}
leftClone = left.clone(); //copy from the left camera
imwrite("left.png", leftClone); // write it to screenshot.png in this directory
rightCam.read(right);
if (right.empty()){cout << "No frame to read" << endl; break;}
rightClone = right.clone(); //copy from the left camera
imwrite("right.png", rightClone); // write it to screenshot.png in this directory
Mat im_left = imread("left.png"); //left cam picture
Mat im_right = imread("right.png"); // right cam picture
Size imagesize = im_left.size();
Mat disparity_left= Mat(imagesize.height,imagesize.width,CV_16S);
Mat disparity_right=Mat(imagesize.height,imagesize.width,CV_16S);
Mat g1,g2,disp,disp8;
cvtColor(im_left,g1, COLOR_BGR2GRAY);
cvtColor(im_right,g2, COLOR_BGR2GRAY);
Ptr<cv::StereoBM> sbm = StereoBM::create(0,21);
sbm->setDisp12MaxDiff(1);
sbm->setSpeckleRange(8);
sbm->setSpeckleWindowSize(9);
sbm->setUniquenessRatio(0);
sbm->setTextureThreshold(507);
sbm->setMinDisparity(-39);
sbm->setPreFilterCap(61);
sbm->setPreFilterSize(5);
sbm->compute(g1,g2,disparity_left);
normalize(disparity_left, disp8, 0, 255, NORM_MINMAX, CV_8U);
namedWindow("Left", WINDOW_AUTOSIZE);
imshow("Left", im_left);
namedWindow("Right", WINDOW_AUTOSIZE);
imshow("Right", im_right);
namedWindow("Depth map", WINDOW_AUTOSIZE);
imshow("Depth map", disp8);
namedWindow("Left Cloned", WINDOW_FREERATIO);
imshow("Left Cloned", leftClone); // left is the left pic taken from camera 0
charCheckForEscKey = waitKey(1);
}
return(0);
}
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include<opencv2/calib3d.hpp>
int main()
{
cv::Mat leftimg =cv::imread("leftimage.jpg");
cv::Mat rightimg = cv::imread("rightimage.jpg");
cv::Mat disparity_left=cv::Mat(leftimg.size(),leftimg.type());
cv::Mat disparity_right=cv::Mat(rightimg.size(),rightimg .type());
cv::Mat g1,g2,disp,disp8;
cv::cvtColor(leftimg,g1,cv::COLOR_BGR2GRAY);
cv::cvtColor(rightimg,g2,cv::COLOR_BGR2GRAY);
cv::Ptr<cv::StereoBM> sbm = cv::createStereoBM(16,21);
sbm->setDisp12MaxDiff(1);
sbm->setSpeckleRange(8);
sbm->setSpeckleWindowSize(9);
sbm->setUniquenessRatio(0);
sbm->setTextureThreshold(507);
sbm->setMinDisparity(-39);
sbm->setPreFilterCap(61);
sbm->setPreFilterSize(5);
sbm->compute(g1,g2,disparity_left);
normalize(disparity_left, disp8, 0, 255, CV_MINMAX, CV_8U);
}

OpenCV: record footage in one window and Display the same video in 2nd window but with contours only

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;
}

How can I convert a cv::Mat to a gray scale in OpenCv?

How can I convert a cv::Mat to a gray scale?
I am trying to run drawKeyPoints func from opencv, however I have been getting an Assertion Filed error. My guess is that it needs to receive a gray scale image rather than a color image in the parameter.
void SurfDetector(cv::Mat img){
vector<cv::KeyPoint> keypoints;
cv::Mat featureImage;
cv::drawKeypoints(img, keypoints, featureImage, cv::Scalar(255,255,255) ,cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
cv::namedWindow("Picture");
cv::imshow("Picture", featureImage);
}
Using the C++ API, the function name has slightly changed and it writes now:
#include <opencv2/imgproc/imgproc.hpp>
cv::Mat greyMat, colorMat;
cv::cvtColor(colorMat, greyMat, CV_BGR2GRAY);
The main difficulties are that the function is in the imgproc module (not in the core), and by default cv::Mat are in the Blue Green Red (BGR) order instead of the more common RGB.
OpenCV 3
Starting with OpenCV 3.0, there is yet another convention.
Conversion codes are embedded in the namespace cv:: and are prefixed with COLOR.
So, the example becomes then:
#include <opencv2/imgproc/imgproc.hpp>
cv::Mat greyMat, colorMat;
cv::cvtColor(colorMat, greyMat, cv::COLOR_BGR2GRAY);
As far as I have seen, the included file path hasn't changed (this is not a typo).
May be helpful for late comers.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 2) {
cout << "Usage: display_Image ImageToLoadandDisplay" << endl;
return -1;
}else{
Mat image;
Mat grayImage;
image = imread(argv[1], IMREAD_COLOR);
if (!image.data) {
cout << "Could not open the image file" << endl;
return -1;
}
else {
int height = image.rows;
int width = image.cols;
cvtColor(image, grayImage, CV_BGR2GRAY);
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
namedWindow("Gray Image", WINDOW_AUTOSIZE);
imshow("Gray Image", grayImage);
cvWaitKey(0);
image.release();
grayImage.release();
return 0;
}
}
}