gpt4 book ai didi

c++ - 在opencv中从Mat图像创建多个子图像?尝试为每个连接的组件创建子图像

转载 作者:行者123 更新时间:2023-11-28 02:56:08 25 4
gpt4 key购买 nike

获取子图像的问题

Mat subImage(output, cv::Rect(x1, y1, x2-x1, y2-y1));
imshow("sub", subImage);

imshow 正在获取第一次的高度和宽度。

FindBlobs(const Mat &binary,vector<vector<Point2i>>&blobs);用于查找连通分量

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

int main()
{
// load the image

Mat img = imread("C:\\Users\\xyz\\Desktop\\text.jpg");
if(!img.data) {
cout << "File not found" << endl;
return -1;
}
//Image scaling
Mat img_scale= imread("C:\\Users\\sujay\\Desktop\\texture.jpeg");
resize(img,img, img_scale.size());

// show it in a window
namedWindow( "Image", WINDOW_AUTOSIZE );
imshow("Image", img);

// image window will immediately disappear if the program ends, so
// we'll wait for a keypress, indefinitely
waitKey();

// first copy the image
Mat img_gray = img.clone();
cvtColor(img, img_gray, CV_RGB2GRAY);
imshow("Image", img_gray);
waitKey();

// do a simple transformation: convert to binary

// first copy the image
Mat img_binary = img.clone();

threshold(img_gray, img_binary, 128.0, 255.0, THRESH_BINARY_INV);
resize(img_binary,img_binary, img_scale.size());
imshow("Image", img_binary);
waitKey();

threshold(img_binary, img_binary, 0.0, 1.0, THRESH_BINARY);
resize(img_binary,img_binary, img_scale.size());

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

vector < vector<Point2i > > blobs;
FindBlobs(img_binary, blobs);
int x1=0,x2=0,y1=0,y2=0;
// 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;
if
if(j == 0)
{
x1=x;
x2=x;
y1=y;
y2=y;
}
else
{
if(x<x1)
x1=x;
if(x>x2)
x2=x;
if(y<y1)
y1=y;
if(y>y2)
y2=y;
}

output.at<Vec3b>(y,x)[0] = b;
output.at<Vec3b>(y,x)[1] = g;
output.at<Vec3b>(y,x)[2] = r;
}
Mat subImage(output, cv::Rect(x1, y1, x2-x1, y2-y1));
imshow("sub", subImage);
}
imshow("labelled", output);
waitKey(0);
return 0;
}
void FindBlobs(const cv::Mat &binary, std::vector < std::vector<cv::Point2i> > &blobs)
{
blobs.clear();
cv::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;
}
cv::Rect rect;
cv::floodFill(label_image, cv::Point(x,y), label_count, &rect, 0, 0, 4);
std::vector <cv::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(cv::Point2i(j,i));
}
}

blobs.push_back(blob);

label_count++;
}
}
}

最佳答案

您没有使用 C++: Mat::Mat(const Mat& m, const Rect& roi)正确,其第二个参数是cv::Rect,即格式[x, y, width, height]

改变

Mat subImage(img_binary, cv::Rect(x1, y1, x2, y2));

Mat subImage(img_binary, cv::Rect(x1, y1, x2-x1, y2-y1));

关于c++ - 在opencv中从Mat图像创建多个子图像?尝试为每个连接的组件创建子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950340/

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