Opencv将数组转化成图像显示出来的方法

    xiaoxiao2021-03-25  127

    楼主是硬件方向,FPGA+ARM架构。最近FPGA加速处理比较火,楼主打算加速下图像处理算法,于是想到了OPENCV。 刚接触,不知道在OPENCV中怎么把一个灰度图像数组转化成图像显示出来,查了好多帖子都答非所问,于是自学了下站长的《OPENCV入门》。 软件环境:win7 64bit                  vs2013+opencv3.1  x64环境下Debug

    代码功能:将一维数组转化成图像输出(灰度图像)。

    Mat Array2Mat(int a[], const char *str); Mat Array2Mat(int a[], const char *str){ Mat M(100,100,CV_8UC1); for (int i = 0; i < M.rows; ++i){ uchar *p = M.ptr<uchar>(i); for (int j = 0; j < M.cols; ++j) p[j] = a[i*wid + j]; } imshow(str, M); return M; } 经测试,成功显示图片。 附上楼主完整C++程序,该程序用作实现sobel边缘检测。

    #include <stdio.h> #include <opencv.hpp> #include <highgui.hpp> #include <core.hpp> #include "sobel.h" using namespace cv; void sobel(int video[N], int sob_x1[N], int sob_y1[N]); int convolution(int operatr[9], int block[9]); Mat Array2Mat(int a[], const char *str); int main(int argc, const char *argv[]) { int i; FILE *fp; int a[N]; //输入的图像向量数组 int sob_x1out[N]; int sob_y1out[N]; //输出向量数组 fopen_s(&fp,"in.txt", "r");//将图像文件in.txt指向fp for (i = 0; i<N; i++){ int tmp; fscanf_s(fp, "%x,", &tmp); a[i] = tmp; } //将in.txt中的测试向量读入a【N】 fclose(fp); Mat inim=Array2Mat(a, "in.txt/jpg"); sobel(a, sob_x1out, sob_y1out); Mat sob_x = Array2Mat(sob_x1out, "sob_x1out"); Mat sob_y = Array2Mat(sob_y1out, "sob_y1out"); cvWaitKey(); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-9301.html

    最新回复(0)