在opencv中显示图像,如MATLAB

时间:2014-10-03 08:42:13

标签: opencv

我正在尝试使用opencv来显示通过在Matlab中指定每个像素的强度而生成的简单图像。但是,当我展示时,结果有差距(参见下图)。有没有办法显示没有显示这些间隙的图像?

#include <stdlib.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char** argv)
{
    n = 100;
    m = 400;
    cv::Mat image(n,m,CV_8UC1);
    for( int i = 0; i < m; i++ )
        image.at<int>(10,i) = 255;
    cv::namedWindow( "Display window", WINDOW_AUTOSIZE );
    // Show our image inside it.
    cv::imshow( "Display window", image );                   
    // Wait for a keystroke in the window
    cv::waitKey(0); 
    return 0;
}

Here is an example image

1 个答案:

答案 0 :(得分:1)

如果您的Mat是CV_8UC1,则必须使用:

image.at<uchar>(10,i) // not int !

enter image description here

int n = 100;
int m = 400;
cv::Mat image(n,m,CV_8UC1);
for( int i = 0; i < m; i++ )
    image.at<uchar>(10,i) = 255;