บทนำ
OpenCV มีเมธอด channels() สำหรับดูจำนวนช่องสีของภาพ (Mat) คืนค่าเป็น integer: 1=Grayscale, 3=BGR/RGB, 4=BGRA/RGBA
ตัวอย่างโค้ด
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "โหลดภาพไม่สำเร็จ" << endl;
return -1;
}
int numChannels = img.channels();
cout << "จำนวนช่องสี = " << numChannels << endl;
// แสดงข้อมูลเพิ่มเติม
cout << "ขนาดภาพ: " << img.cols << "x" << img.rows << endl;
cout << "ชนิดข้อมูล: " << img.type() << endl;
cout << "Depth: " << img.depth() << endl;
system("pause");
waitKey(0);
return 0;
}
ค่าที่เป็นไปได้ของ channels()
| ค่า | ความหมาย | ตัวอย่าง |
|---|---|---|
| 1 | Single Channel (Grayscale) | CV_8UC1, CV_32FC1 |
| 2 | 2 Channels | Optical Flow (x,y), Complex numbers |
| 3 | 3 Channels (BGR, RGB, HSV, Lab, YCrCb) | CV_8UC3 - ภาพสีมาตรฐาน |
| 4 | 4 Channels (BGRA, RGBA) | CV_8UC3 - ภาพมี Alpha channel |
