2012-02-08 105 views
4

所有像素坐标我想有相同的颜色作为一个像素获取具有相同的颜色

List<cv::Point> GetAllPixels(cv::Point x) 
{ 
//imp 
} 

我怎样才能EmguCV或OpenCV的做这一切的像素坐标?

回答

5

下面是使用的OpenCV C++一个可能的解决方案:

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 
#include <vector> 

using namespace std; 
using namespace cv; 

std::vector<cv::Point> getPixelsWithColor(const cv::Mat& src, cv::Scalar targetColor) 
{ 
    assert(src.type() == CV_8UC3); 

    std::vector<cv::Point> identicalPoints; 

    for(int row = 0; row < src.rows; ++row) 
    { 
     for(int col = 0; col < src.cols; ++col) 
     { 
      cv::Vec3b currPixel = src.at<cv::Vec3b>(row, col); 
      if(currPixel[0] == targetColor.val[0] && 
       currPixel[1] == targetColor.val[1] && 
       currPixel[2] == targetColor.val[2]) 
      { 
       identicalPoints.push_back(cv::Point(col, row)); 
      } 
     } 
    } 

    return identicalPoints; 
} 

int main(int argc, char** argv) 
{ 
    Mat squares = imread("colored_squares.png"); 

    const Scalar RED = Scalar(0, 0, 255); // OpenCV is BGR order... 
    vector<Point> redPixelLocations; 
    redPixelLocations = getPixelsWithColor(squares, RED); 

    Mat redMask = Mat::zeros(squares.size(), CV_8UC1); 

    vector<Point>::iterator i; 
    for(i = redPixelLocations.begin(); i != redPixelLocations.end(); ++i) 
    { 
     redMask.at<uchar>(*i) = 255; 
    } 

    imshow("Squares", squares); 
    imshow("Red mask", redMask); 
    waitKey(); 

    return 0; 
} 

我用这个输入图像:
enter image description here

并取得该输出图像:
enter image description here

享受! :)

0

答案应该让你开始。也可以考虑使用Cmp功能

对于IplImage它可能是这样的:

IplImage *img, *col_img, *mask_img; 
// initialise images appropriately... 
svSet(col_img, [colour of your choise]); 
cvCmp(img, col_img, mask_img, CV_CMP_EQ); 

使用mask_img提取坐标。您可能会发现cv::Mat的等效操作。