0

我有一个灰色图像,我想从每 8 个像素一起获取像素值(像素块) 我如何从灰色图像中读取这个像素块?这是我制作的代码

void main()

{
 Mat img = imread("Frame.jpg",0);

if(!img.data)   
    std::cout << "error";

double minVal, maxVal;
minMaxLoc(img,&minVal,&maxVal,NULL,NULL);
double Threshold = 0.5 * (minVal + maxVal);
int i,j;
for (i=0;i<img.rows;i++)
    for(j=0;j<img.cols;j++)
        unsigned char Pixel = img.at<unsigned char>(i,j);




namedWindow( "Display window", CV_NORMAL );// Create a window for display.
    imshow( "Display window", img );
 waitKey(0);    

     }

谢谢

4

1 回答 1

0

不确定您到底要做什么,但是,如果您要获取中心像素周围的像素,您可以通过调整索引来手动完成。如果像素周围的块被标记为:

0 1 2
3 P 4
5 6 7

您可以通过在循环中执行此操作来获取 0-7 的值

for (i=1;i<img.rows-1;i++){
    for(j=1;j<img.cols-1;j++){
        unsigned char Pixel = img.at<unsigned char>(i,j);
        unsigned char Pixel_0 = img.at<unsigned char>(i-1,j-1);
        unsigned char Pixel_1 = img.at<unsigned char>(i,j-1);
        unsigned char Pixel_2 = img.at<unsigned char>(i+1,j-1);
        unsigned char Pixel_3 = img.at<unsigned char>(i-1,j);
        unsigned char Pixel_4 = img.at<unsigned char>(i+1,j);
        unsigned char Pixel_5 = img.at<unsigned char>(i-1,j+1);
        unsigned char Pixel_6 = img.at<unsigned char>(i,j+1);
        unsigned char Pixel_7 = img.at<unsigned char>(i+1,j+1);
    }
}

或者,如果您想将整个窗口子集到一个新的 cv::Mat 中,您可以使用 ROI。

for (i=1;i<img.rows-1;i++){
    for(j=1;j<img.cols-1;j++){
        unsigned char Pixel = img.at<unsigned char>(i,j);
        cv::Mat ROI = img(cv::Rect(i-1,j-1,3,3));
    }
}

请注意,对于这两种情况,循环仅在 1->rows-1 和 1->cols-1 之间,您必须分别处理边界情况。

于 2013-11-11T19:24:37.913 回答