在单个轮廓中获得多于一个像素的值

时间:2015-01-08 10:14:02

标签: c++ opencv image-processing feature-extraction connected-components

对于单个标签的连接组件标记后你好,为什么我得到的像素值超过一个?

这是我的形象

enter image description here

#include <iostream>
#include <vector>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

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

int main(int argc, char **argv)
{
    cv::Mat img = cv::imread("/Users/Rodrane/Documents/XCODE/test/makalesvm/persembe.png", 0);     if(!img.data) {
        std::cout << "File not found" << std::endl;
        return -1;
    }

    cv::namedWindow("binary");
    cv::namedWindow("labelled");

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

    cv::Mat binary;
    std::vector < std::vector<cv::Point2i > > blobs;
    equalizeHist( img  , img );

    cv::threshold(img, binary, 0, 1, cv::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<cv::Vec3b>(y,x)[0] = b;
            output.at<cv::Vec3b>(y,x)[1] = g;
            output.at<cv::Vec3b>(y,x)[2] = r;
        }
    }


    cout << "H = "<< endl << " "  << output << endl << endl;
   cv::imshow("binary", img);
    cv::imshow("labelled", output);
    cv::waitKey(0);

    return 0;
}

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

    // Fill the label_image with the blobs
    // 0  - background
    // 1  - unlabelled foreground
    // 2+ - labelled foreground

    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++;
        }
    }

}

实际上,当我打印图像时,这是一个单一的标签

enter image description here

然而,当我检查像素值时。我看到实际上有2个不同的值(我只写下矩阵的一部分而不是全部)

  

H = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0 192 33 ,0, 192 ,< strong> 33 ,0, 192 33 ,0,0,0,0,]

也可以将这行代码添加到 FindBlobs 函数的最后一行我收到 3 ,因为 label_count 变量从开始2 这也证明 H 是单个标签。

 cout << "number of labels = "<< endl << " "  << label_count << endl << endl;

0 个答案:

没有答案