gpt4 book ai didi

opencv - 使用 Opencv 去除小 Blob

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:55 24 4
gpt4 key购买 nike

我在 visual studio 2010 上使用 opencv2.2。我编写了一段代码来为 OCR 预处理图像。我使用了很多轨迹栏来改变参数。预处理功能之一是通过绘制轮廓并根据大小过滤掉小 Blob 。但是,当我运行程序时,cvDrawContours 函数给我一个错误。基本上我得到弹出窗口和一个错误说 R6010 -abort 已被调用。命令行说 matrix.cpp 中有一个未知的数组类型在第 641 行。我在这里包括我的代码。该问题由 BlobFunc 函数中的 cvDrawContours 函数调用。

#include <C:\OpenCV2.2\modules\core\include\opencv2\core\core.hpp>  
#include <C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui.hpp>
#include <iostream>
#include <string.h>
#include <C:\OpenCV2.2\include\opencv\cv.h>
#include <stdlib.h>
#include <C:\OpenCV2.2\modules\highgui\include\opencv2\highgui\highgui_c.h>
#include <C:\Users\Administrator\Documents\blobs\blob.h>
#include <C:\Users\Administrator\Documents\blobs\BlobResult.h>

using namespace cv;
using namespace std;

int MAX_KERNEL_LENGTH = 30;
int counter=0;
int Blurtype=0;
int Kern=5;
int *Kernp=&Kern;
int betaval=50;
int *betavalp=&betaval;
int Threshtype=0;
int Threshval=30;
int size=10;

Mat src1, src2, dst1, dst2, dst3;
CvScalar black=CV_RGB( 0, 0, 0 ); // black color
CvScalar white=CV_RGB( 255, 255, 255 ); // white color
double area;

void BlobFunc(int,void*);
int main( int argc, char** argv )
{
//Read Input
src1 = imread(argv[1]);
if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
//Create Windows
namedWindow("Original Image",1);
namedWindow("Binarized Image",1);
namedWindow("Gray Image",1);
namedWindow("Sharpened Image",1);
namedWindow("Blurred Image",1);
imshow("Original Image",src1);
namedWindow("Blobs",1);
//Create Trackbars
cvtColor(src1,src2,CV_RGB2GRAY);
imshow("Gray Image",src2);
threshold( src2, dst1, Threshval, 255,Threshtype);
imshow("Binarized Image",dst1);
createTrackbar("Kernel","Blurred Image",&Kern,MAX_KERNEL_LENGTH,BlobFunc);
createTrackbar("BlurType","Blurred Image",&Blurtype,3,BlobFunc);
createTrackbar("Betaval","Sharpened Image",&betaval,100,BlobFunc);
createTrackbar("Threshold value","Binarized Image",&Threshval,255,BlobFunc);
createTrackbar("Type","Binarized Image",&Threshtype,4,BlobFunc);
createTrackbar("Size","Blobs",&size,100,BlobFunc); //Size of Blob
waitKey(0);
return 0;
}

void BlobFunc(int,void*)
{
CvMemStorage *storage=cvCreateMemStorage(0);
CvSeq *contours=0;
cvtColor(src1,src2,CV_RGB2GRAY);
imshow("Gray Image",src2);
threshold( src2, dst1, Threshval, 255,Threshtype);
imshow("Binarized Image",dst1);
for ( int i = 1; i < Kern; i = i + 2 )
{
if (Blurtype==0)
{
blur(dst1,dst2, Size( i, i ), Point(-1,-1) );
}
else if (Blurtype==1)
{
GaussianBlur( dst1, dst2, Size( i, i ), 0, 0 );
}
else if (Blurtype==2)
{
medianBlur ( dst1, dst2, i );
}
else if (Blurtype==3)
{
bilateralFilter ( dst1, dst2, i, i*2, i/2 );
}
}
imshow("Blurred Image",dst2);
addWeighted( dst1, 1, dst2, -double(betaval)/100, 0.0, dst3);
imshow("Sharpened Image",dst3);
IplImage img=dst3;
cvFindContours(&img,storage,&contours,sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
IplImage *img_out=cvCreateImage( cvGetSize(&img), 8, 3 );
cvZero( &img_out );
for( ; contours != 0; contours = contours->h_next )
{
cvDrawContours( &img_out, contours, black, black, -1, CV_FILLED, 8 );
}

Mat imgout=img_out;
cvReleaseMemStorage( &storage );
imshow("Blobs",imgout);
}

最佳答案

当我传递无效的 Mat(或 iplimage)时,我得到了这种类型的错误。可能是我复制错了,或者图像可能是一种类型、 channel 、颜色数量等。

问题似乎在于参数不正确,所以我会看看你是如何传递图像的,即而不是 &img,你可以试试不带 & 的 img。恐怕我不熟悉 C++,但这就是我开始的地方。

下面的例子是类似的,也许它会帮助展示 & 和 * 的使用。

void displayContours(const Mat &src, Mat features) {
RNG rng(12345);
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat s;
src.copyTo(s);
findContours(s, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// Approximate contours to polygons + get bounding rects and circles
vector<vector<Point> > contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point2f> center(contours.size());
vector<float> radius(contours.size());

for (int i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contours_poly[i]));
minEnclosingCircle(contours_poly[i], center[i], radius[i]);
}

/// Draw polygonal contour + bonding rects + circles
for (int i = 0; i < contours.size(); i++) {
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
rng.uniform(0, 255));
drawContours(features, contours_poly, i, color, 1, 8, vector<Vec4i>(),
0, Point());
rectangle(features, boundRect[i].tl(), boundRect[i].br(), color, 2, 8,
0);
circle(features, center[i], (int) radius[i], color, 2, 8, 0);
}

}

这是来自 opencv 教程之一,使用的是 Mat 而不是 iplImage,这可能需要不同的用法。

关于opencv - 使用 Opencv 去除小 Blob ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com