範例程式:C:\OpenCV2.4\samples\cpp\tutorial_code\introduction\display_image\display_image.cpp (內文的程式碼有稍作修改)
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using namespace std; int main( int argc, char** argv ) { Mat image; if( argc != 2) { cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; image = imread("lena.jpg", CV_LOAD_IMAGE_COLOR); // Read the file } else { image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file } if(! image.data ) // Check for invalid input { cout << "Could not open or find the image" << endl; system("pause"); return -1; } namedWindow( "Lena - Display window", CV_WINDOW_AUTOSIZE );// Create a window for display. imshow( "Lena - Display window", image ); // Show our image inside it. waitKey(0); // Wait for a keystroke in the window return 0; }imread(const string& filename, int flags=1)
- filename – Name of file to be loaded.
- flags – Flags specifying the color type of a loaded image:
>0 Return a 3-channel color image
=0 Return a grayscale image
<0 Return the loaded image as is.
- Windows bitmaps - *.bmp, *.dib (always supported)
- JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
- JPEG 2000 files - *.jp2 (see the Notes section)
- Portable Network Graphics - *.png (see the Notes section)
- Portable image format - *.pbm, *.pgm, *.ppm (always supported)
- Sun rasters - *.sr, *.ras (always supported)
- TIFF files - *.tiff, *.tif (see the Notes section)
以下是比較簡單的範例,實作讀取圖片並儲存。
int main() { IplImage *InImage; InImage = cvLoadImage("lena.jpg",-1); CvSize Size1 = cvGetSize(InImage); //建立視窗(視窗名稱,參數) cvNamedWindow("InImage",1); //顯示影像(視窗名稱,影像檔案) cvShowImage("InImage",InImage); cvWaitKey(0); //按下任意按鍵可將圖片關閉 //儲存影像(欲儲存名稱, 影像檔案) cvSaveImage("saved.jpg", InImage); return 0; }
沒有留言:
張貼留言