为什么openCV中的输入图像findContours()要转灰度?
Why is the input image findContours() in openCV has to be converted to grayscale?
我正在关注以下站点上的教程:
http://harismoonamkunnu.blogspot.co.uk/2013/06/opencv-find-biggest-contour-using-c.html
如果我遵循代码,它会完美运行。但是,我所做的是输入一个灰度图像(由 photoshop 灰度并保存为 jpg)并尝试跳过这一步:
cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
当我再次 运行 Visual Studio 中的相同代码时,我收到 运行 时间错误显示:
Unhandled exception at 0x00007FFE56A79E08 in image_capture.exe:
Microsoft C++ exception: cv::Exception at memory location
0x000000C1D58FE1F0. occurred
在 countours.cpp 文件:
scanner = cvStartFindContours( img, storage, cntHeaderSize, mode, method, offset );
我想尽量避免使用 cvtColor,因为在我的应用程序中,我的输入图像是灰度图片。我的代码如下:
#include "stdafx.h"
#include <iostream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv\cv.h"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{
int largest_area=0;
int largest_contour_index=0;
Rect bounding_rect;
Mat src = imread("src.jpg"); //Load source image
Mat thr(src.rows,src.cols,CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
//cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
threshold(src, src,25, 255,THRESH_BINARY); //Threshold the gray
vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
{
double a=contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
Scalar color( 255,255,255);
drawContours( dst, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0,255,0),1, 8,0);
imshow( "src", src );
imshow( "largest Contour", dst );
waitKey(0);
}
知道如何避免 cvtColor() 吗?
谢谢
根据 opencv documentation,findContour
函数的输入图像必须是 8 位单通道,因此您必须通过 threshold
、[=12] 将输入图像更改为该类型=],等等。如果您的图像是灰度图像,只需以 CV_LOAD_IMAGE_GRAYSCALE
模式加载它。
cv::Mat rawImg = imread(imgName, CV_LOAD_IMAGE_GRAYSCALE);
我正在关注以下站点上的教程:
http://harismoonamkunnu.blogspot.co.uk/2013/06/opencv-find-biggest-contour-using-c.html
如果我遵循代码,它会完美运行。但是,我所做的是输入一个灰度图像(由 photoshop 灰度并保存为 jpg)并尝试跳过这一步:
cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
当我再次 运行 Visual Studio 中的相同代码时,我收到 运行 时间错误显示:
Unhandled exception at 0x00007FFE56A79E08 in image_capture.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000C1D58FE1F0. occurred
在 countours.cpp 文件:
scanner = cvStartFindContours( img, storage, cntHeaderSize, mode, method, offset );
我想尽量避免使用 cvtColor,因为在我的应用程序中,我的输入图像是灰度图片。我的代码如下:
#include "stdafx.h"
#include <iostream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv\cv.h"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{
int largest_area=0;
int largest_contour_index=0;
Rect bounding_rect;
Mat src = imread("src.jpg"); //Load source image
Mat thr(src.rows,src.cols,CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
//cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
threshold(src, src,25, 255,THRESH_BINARY); //Threshold the gray
vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
{
double a=contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
Scalar color( 255,255,255);
drawContours( dst, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0,255,0),1, 8,0);
imshow( "src", src );
imshow( "largest Contour", dst );
waitKey(0);
}
知道如何避免 cvtColor() 吗?
谢谢
根据 opencv documentation,findContour
函数的输入图像必须是 8 位单通道,因此您必须通过 threshold
、[=12] 将输入图像更改为该类型=],等等。如果您的图像是灰度图像,只需以 CV_LOAD_IMAGE_GRAYSCALE
模式加载它。
cv::Mat rawImg = imread(imgName, CV_LOAD_IMAGE_GRAYSCALE);