opencv1.0 iplimage與二維陣列相互轉換
阿新 • • 發佈:2019-01-13
#include "stdafx.h" #include <cv.h> #include <highgui.h> int main(int argc, char* argv[]) { IplImage *src = cvLoadImage("lena.jpg", 0); cvNamedWindow("source", 1); cvShowImage("source", src); cvWaitKey(); int m = src->height; int n = src->width; int i, j; printf("channel = %d", src->nChannels); // 分配記憶體 int **p; p = new int *[m]; for (i=0; i<m; i++) { p[i] = new int[n]; } // 將影象資料存到二位陣列中 //CvScalar s; //for (i=0; i<m; i++) //{ // for (j=0; j<n; j++) // { // s = cvGet2D(src, i, j); // p[i][j] = s.val[0]; // } //} /**************************************/ // 或者 uchar *ptr; for (i=0; i<m; i++) { ptr = (uchar*)src->imageData + i*src->widthStep; for (j=0; j<n; j++) { p[i][j] = (int)*(ptr+j); } } /**************************************/ // 將二位陣列中的資料載入新的單通道灰度影象中,看是否和原圖一致 IplImage *copy = cvCreateImage(cvGetSize(src), 8, 1); for (i=0; i<m; i++) { for (j=0; j<n; j++) { cvSetReal2D(copy, i, j, p[i][j]); } } // 顯示影象副本 cvNamedWindow("copy", 1); cvShowImage("copy", copy); cvWaitKey(); // 釋放影象 cvReleaseImage(&src); cvReleaseImage(©); cvDestroyAllWindows(); //釋放記憶體 for (i=0; i<m; i++) delete [] p[i]; delete [] p; return 0; }
轉:https://blog.csdn.net/jacke121/article/details/58621665