代码功能:将一维数组转化成图像输出(灰度图像)。
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; }