C++ 为什么必须将openCV中的输入图像findContours()转换为灰度?
我正在以下网站上关注教程: 如果我遵循代码,它会完美工作。然而,我所做的是输入一个灰度图像(由photoshop进行灰度缩放并保存为jpg),然后我尝试跳过以下步骤:C++ 为什么必须将openCV中的输入图像findContours()转换为灰度?,c++,opencv,opencv3.0,C++,Opencv,Opencv3.0,我正在以下网站上关注教程: 如果我遵循代码,它会完美工作。然而,我所做的是输入一个灰度图像(由photoshop进行灰度缩放并保存为jpg),然后我尝试跳过以下步骤: cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray 当我再次在Visual Studio中运行相同的代码时,我收到一个运行时错误,显示: image_capture.exe中0x00007FFE56A79E08处未处理的异常: 微软C++异常:CV::内存位置异常 0x000000
cvtColor(src,thr,CV_BGR2GRAY); //Convert to gray
当我再次在Visual Studio中运行相同的代码时,我收到一个运行时错误,显示:
image_capture.exe中0x00007FFE56A79E08处未处理的异常:
微软C++异常:CV::内存位置异常
0x000000C1D58FE1F0。发生
在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);
}
#包括“stdafx.h”
#包括
#包括“opencv2\highgui\highgui.hpp”
#包括“opencv\cv.h”
#包括“opencv2\imgproc\imgproc.hpp”
使用名称空间cv;
使用名称空间std;
int main()
{
int最大面积=0;
int最大轮廓指数=0;
矩形边界;
Mat src=imread(“src.jpg”);//加载源映像
材料厚度(src.行、src.列、CV_8UC1);
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0));
//CVT颜色(src、thr、CVBGr2灰色);//转换为灰色
阈值(src,src,25255,THRESH_BINARY);//灰度阈值
矢量轮廓;//用于存储轮廓的矢量
向量层次;
查找轮廓(src、轮廓、层次、CV_RETR_CCOMP、CV_CHAIN_APPROX_SIMPLE);//在图像中查找轮廓
对于(int i=0;i最大面积){
最大面积=a;
最大轮廓指数=i;//存储最大轮廓指数
bounding_rect=boundingRect(轮廓[i]);//查找最大轮廓的边界矩形
}
}
标量颜色(255255);
drawContours(dst、等高线、最大等高线索引、颜色、CV填充、8、层次);//使用以前存储的索引绘制最大等高线。
矩形(src,边界矩形,标量(0255,0),1,8,0);
imshow(“src”,src);
imshow(“最大轮廓”,dst);
等待键(0);
}
知道如何避免cvtColor()吗
谢谢根据,输入图像到findContour
函数必须是8位单通道,因此您必须通过阈值
,cvtColor
等将输入图像更改为该类型。如果您的图像是灰色图像,只需在CV\u load\u image\u灰度
模式下加载即可
cv::Mat rawImg=imread(imgName、cv\u LOAD\u IMAGE\u灰度)代码>问题在于图像的像素值可能为灰度,但图像本身有多个通道,最有可能是3个通道。findContour
功能仅接受1个通道图像。只需确保您的图像只有一个通道,这可以通过img.shape
轻松验证,如果它有多个通道,那么您可以对多通道矩阵进行切片,以获取表示输入灰度图像的任何组成通道。Mat src=imread(“src.jpg”,cv::imread\u GRAYSCALE)代码>@ZdaR我是openCV新手。你能给我一些关于如何做到这一点的指导吗?我只能找到整形()方法。