Opencv中灰度影象和imread的區別
阿新 • • 發佈:2019-01-29
/*******************************************************/ /*遍歷一個資料夾中的100張圖片並把他們全部轉為8bit的影象*/ /*******************************************************/ #include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; //define Width and Height const int width = 256; const int height = 256; int main() { Mat src; int i; string src_image_name = "C:\\Users\\Administrator\\Desktop\\zhuan\\before\\"; string src_image_name_after = "C:\\Users\\Administrator\\Desktop\\zhuan\\after\\"; string src_image_name1 = src_image_name; char num[12], num1[4] = "1 (", num2[6] = ").png"; string num3 = ".png"; for (i = 1; i <= 100; i++) { _itoa_s(i, num, 10); src_image_name += num1; src_image_name += num; src_image_name += num2; cout << src_image_name << endl; Mat grayImage = imread(src_image_name, 0); imwrite(src_image_name_after + num + num3, grayImage); //把圖片寫入到圖片中 //cvWaitKey(1000); src_image_name = src_image_name1; } waitKey(0); system("pause"); return 0; }
二:思考:
1.單通道是指每個畫素點只有一個灰度值,灰度圖是隻有一種採用樣色的影象,如果一個RGB的3通道圖,每一點的顏色都是R=G=B,那麼它也是一幅灰度影象,但是在Opencv中單通道圖就是灰度圖。
2.彩色轉灰度
/*****************************************************/ //使用opencv中的cvtColor函式將彩色圖片轉化成灰度圖片。 /*****************************************************/ #include "cv.h" // OpenCV 檔案頭 #include "highgui.h" #include "cvaux.h" #include "cxcore.h" #include "opencv2/opencv.hpp" #include "opencv2/imgproc.hpp" #include <iostream> #include <string> using namespace cv; using namespace std; int main() { string imageName("C:\\Users\\Administrator\\Desktop\\11.jpg"); // 圖片在電腦中的絕對地址 Mat image, resImage; //Mat是OpenCV最基本的資料結構,這是定義一個影象矩陣型別 image = imread(imageName.c_str(), IMREAD_COLOR); //讀入圖片資料 if (image.empty()) //讀取失敗時 { cout << "Could not open or find the image" << std::endl; return -1; } namedWindow("原圖", WINDOW_AUTOSIZE); // 建立一個視窗 imshow("原圖", image); // 在視窗中顯示圖片 cvtColor(image, resImage, CV_RGB2GRAY); //把圖片轉化為灰度圖 //把圖片寫入到圖片中 imwrite("C:\\Users\\Administrator\\Desktop\\111.jpg", resImage); namedWindow("灰度圖", WINDOW_AUTOSIZE); // 建立一個視窗 imshow("灰度圖", resImage); // 在視窗中顯示圖片 waitKey(0); // 等待一次按鍵,程式結束 return 0; }
3.imread的用法區別
文章主要引用自:http://blog.csdn.net/yang6464158/article/details/20903415(改變資料夾中影象尺寸大小(resize))/*****************************************************/ //三種imread對應的不同通道影象 /*****************************************************/ #include<iostream> #include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv2\imgproc\imgproc.hpp> #include<opencv2\opencv.hpp> using namespace std; using namespace cv; int main() { //載入一張彩色圖片並顯示 Mat srcImage = imread("C:\\Users\\Administrator\\Desktop\\11.jpg", 1); namedWindow("彩色", WINDOW_AUTOSIZE); imshow("彩色", srcImage); int nHeight = srcImage.rows; int nWidth = srcImage.cols; cout << "彩色影象的高度" << nHeight << endl; cout << "彩色影象的寬度" << nWidth << endl; cout << "彩色的通道數" << srcImage.channels() << endl; //彩色圖片的通道數 //這種方法讀入的是三通道的灰度圖 當我們在使用imread()函式而沒有給出第二個引數的時候,第二個引數的預設值是IMREAD_COLOR,即預設讀入的圖片是彩色圖片。 //雖然是一張灰度圖片,但是預設以彩色的方式讀入,所以三個通道的值都取了該畫素對應的灰度值。 Mat img = imread("C:\\Users\\Administrator\\Desktop\\22.png"); namedWindow("多通道灰度", WINDOW_AUTOSIZE); imshow("多通道灰度", img); cout << "多通道灰度的通道數" << img.channels() << endl; //灰度圖片的通道數 http://www.th7.cn/Program/cp/201605/854881.shtml //載入一張單通道的灰度圖,第二個引數必須是0 Mat grayImage = imread("C:\\Users\\Administrator\\Desktop\\22.png", 0); imwrite("C:\\Users\\Administrator\\Desktop\\222.jpg", grayImage); //把圖片寫入到圖片中 namedWindow("單通道灰度", WINDOW_AUTOSIZE); imshow("單通道灰度", grayImage); cout << "單通道灰度的通道數" << grayImage.channels() << endl; //灰度圖片的通道數 cvWaitKey(0); return 0; }