如何保存灰度图像的黑色像素并将其绘制到相同大小的另一个灰度图像?

时间:2013-05-07 14:36:25

标签: c++ opencv contour

我正在尝试保存原始灰度图像的黑色像素位置,之后想要将其绘制到灰度图像的修改版本。我的代码在这里:

std::vector< std::vector <cv::Point> > Black_Pixel;
    for(int y = 0; y < Original_Image.rows; y++)
        {
            for(int x = 0; x < Original_Image.cols; x++)
                {               
                    if (Original_Image.at<unsigned char>(x,y) == 0 )
                        {
                             Black_Pixel[0].push_back(cv::Point(x,y));
                             Black_Pixel.push_back( std::vector <cv::Point>());
                         }                                      
                }
        }

    for( unsigned int idx = 0; idx < Black_Pixel.size(); idx++ )
        {
            New_image.at<unsigned char>(Black_Pixel[0][idx].x,Black_Pixel[0][idx].y) = 0;
            cv::waitKey();
        }

2 个答案:

答案 0 :(得分:1)

试试这个: row.push_back(CV ::点(X,Y));

实际上,拿走一行,而不是这样:

Black_Pixel[0].push_back(cv::Point(x,y)); 

当然首先需要初始化Black_Pixel中的第0个元素:

Black_Pixel.push_back(std::vector<cv::Point>);

答案 1 :(得分:1)

std::vector <cv::Point> Black_Pixel;
for(int y = 0; y < Original_Image.cols; y++)
    {
        for(int x = 0; x < Original_Image.rows; x++)
            {               
                if (Original_Image.at<unsigned char>(x,y) == 0 )
                    {
                         Black_Pixel.push_back(cv::Point(x,y));
                    }                                      
            }
    }

for( unsigned int idx = 0; idx < Black_Pixel.size(); idx++ )
    {
        New_image.at<unsigned char>(Black_Pixel[idx]) = 0;           
    }

cv::imshow("New Image", New_image);