使用 waitKey () 时无法打印像素值

Cannot print a pixel value when using waitKey()

本文关键字:打印 像素 waitKey 使用      更新时间:2023-10-16

我正在编写一个代码来将蒙版应用于图像。在编写代码的中途,我意识到:

//applying a mask throughout the image using user defined function.
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
void sharpen(Mat& src, Mat& dst)
{ cout<<"nnFunction just callednn";
/*for (int i=1; i<src.rows-1;i++)
    {
        for (int j=1; j<src.cols-1;j++)
        {
            //dst(i,j)=(-1*(src(i-1,j-1)+src(i-1,j)+src(i-1,j+1)+src(i,j-1)+src(i,j+1)+src(i+1,j-1)+src(i+1,j)+src(i+1,j+1))+8*src(i,j))/9;
        }
    }*/
dst=src;
imshow("src",src);
imshow("dst",dst);
//cout<<src;
}
int main()
{
Mat src,dst;
src=imread("/home/krishna/Downloads/door1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
namedWindow("src",WINDOW_NORMAL);
namedWindow("dst",WINDOW_NORMAL);
cout<<"nnHinn";
cout<<(int)src.at<uchar>(155,155);
//sharpen(src,dst);
//waitKey(-1);
return 0;
}

如果我取消注释waitKey和/或sharpen(),则不会打印(155,155(处的像素。我尝试更改src.at<>()的数据类型,但徒劳无功...

更改此行:

cout<<(int)src.at<uchar>(155,155);

由:

cout<<(int)src.at<uchar>(155,155)<<endl;

std::endl将插入换行符并刷新流。

您也可以只使用std::flush来刷新输出流。