gpt4 book ai didi

c++ - 在图像上只找到大 Blob

转载 作者:行者123 更新时间:2023-11-28 00:19:50 26 4
gpt4 key购买 nike

您好,我正在尝试在此图片上查找字符。

这是经过一些预处理后我收到的图像。

现在我正在尝试进行连接组件标记以查找 Blob 。但是我也有很多小 Blob 。

enter image description here

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

void FindBlobs(const Mat &binary, vector < vector<Point2i> > &blobs);

int main(int argc, char **argv)
{

Mat img = imread("adaptive.png", 0);

if(!img.data) {
cout << "File not found" << endl;
return -1;
}

namedWindow("binary");
namedWindow("labelled");

Mat output = Mat::zeros(img.size(), CV_8UC3);

Mat binary;
vector < vector<Point2i > > blobs;

threshold(img, binary, 0, 1, THRESH_BINARY_INV);


FindBlobs(binary, blobs);

// Randomy color the blobs
for(size_t i=0; i < blobs.size(); i++) {
unsigned char r = 255 * (rand()/(1.0 + RAND_MAX));
unsigned char g = 255 * (rand()/(1.0 + RAND_MAX));
unsigned char b = 255 * (rand()/(1.0 + RAND_MAX));

for(size_t j=0; j < blobs[i].size(); j++) {
int x = blobs[i][j].x;
int y = blobs[i][j].y;
output.at<Vec3b>(y,x)[0] = b;//Vec3b RGB color order
output.at<Vec3b>(y,x)[1] = g;
output.at<Vec3b>(y,x)[2] = r;
}
}





imshow("binary", img);
imshow("labelled", output);
waitKey(0);

return 0;
}

void FindBlobs(const Mat &binary, vector < vector<Point2i> > &blobs)
{
blobs.clear();



Mat label_image;
binary.convertTo(label_image, CV_32SC1);

int label_count = 2; // starts at 2 because 0,1 are used already

for(int y=0; y < label_image.rows; y++) {
int *row = (int*)label_image.ptr(y);
for(int x=0; x < label_image.cols; x++) {
if(row[x] != 1) {
continue;
}

Rect rect;

floodFill(label_image, Point(x,y), label_count, &rect, 0, 0, 4);

vector <Point2i> blob;

for(int i=rect.y; i < (rect.y+rect.height); i++) {
int *row2 = (int*)label_image.ptr(i);
for(int j=rect.x; j < (rect.x+rect.width); j++) {
if(row2[j] != label_count) {
continue;
}
blob.push_back(Point2i(j,i));

}
}

blobs.push_back(blob);

label_count++;
}
}

}

所以通过这个算法我收到了 blob

enter image description here

但是当我这样做的时候

if(blobs.size()>50) {
blob.push_back(Point2i(j,i));
}

我收到黑屏。然而,当我尝试

 if(blob.size()<50){
blob.push_back(Point2i(j,i));
}

我收到小 Blob ,这里的实际问题是什么?

enter image description here

最佳答案

猜猜你想存储那些“大”blob?

如果是,修改下面的代码

blobs.push_back(blob);
label_count++;

为此:

if(blob.size() > 50){
blobs.push_back(blob);
}
label_count++;

你可以收到这样的图片:

enter image description here

关于c++ - 在图像上只找到大 Blob ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065617/

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