ภาพดิจิทัลประกอบด้วยพิกเซล การใช้ OpenCV ทำให้ง่ายต่อการอ่านค่าของพิกเซล อย่างไรก็ตาม หากต้องการได้ค่าพิกเซล เราต้องแยกช่องสัญญาณแยกกัน
ที่นี่เรากำลังโหลดรูปภาพในเมทริกซ์ชื่อ 'cimage' จากนั้นมันจะแปลงรูปภาพโดยใช้ 'cvtColor(cimage, img, COLOR_BGR2GRAY); ' และเก็บไว้ในเมทริกซ์ชื่อ 'img'
โปรแกรมต่อไปนี้จะอ่านค่าพิกเซลของรูปภาพและแสดงค่าในหน้าต่างคอนโซล
ตัวอย่าง
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
int x;//Declaring an integer variable to hold values of pixels//
Mat cimage = imread("colors.jpg");//loading an image//
Mat img;//Declaring an empty matrix to store converted image//
cvtColor(cimage, img, COLOR_BGR2GRAY);//Converting loaded image to grayscale image//
for (int i = 0; i < img.rows; i++)//loop for rows// {
for (int j = 0; j < img.cols; j++)//loop for columns// {
x = (int)img.at<uchar>(i, j);//storing value of (i,j) pixel in variable//
cout << "Value of pixel" << "(" << i << "," << j << ")" << "=" << x << endl;//showing the values in console window//
}
}
imshow("Show", img);//showing the image//
waitKey();//wait for keystroke from keyboard//
return 0;
} ผลลัพธ์
