หากต้องการอ่านค่าของพิกเซลที่ระบุ เราสามารถใช้วิธี 'at' หรือ 'direct access' เราจะได้เรียนรู้ทั้งสองแนวทางที่นี่
เริ่มต้นด้วยวิธี 'at' โปรแกรมต่อไปนี้จะอ่านค่าพิกเซลที่ (10, 29) ของภาพ RGB
ตัวอย่าง
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat image;//taking an image matrix//
image = imread("sky.jpg");//loading an image//
int x = image.at<Vec3b>(10, 29)[0];//getting the pixel values//
int y = image.at<Vec3b>(10, 29)[1];//getting the pixel values//
int z = image.at<Vec3b>(10, 29)[2];//getting the pixel values//
cout << "Value of blue channel:" << x << endl;//showing the pixel values//
cout << "Value of green channel:" << x << endl;//showing the pixel values//
cout << "Value of red channel:" << x << endl;//showing the pixel values//
system("pause");//pause the system to visualize the result//
return 0;
} ผลลัพธ์

ผลลัพธ์ของโปรแกรมจะแสดงขึ้นในหน้าต่างคอนโซล โดยใช้สามบรรทัดต่อไปนี้ เราจะได้ค่ารูปแบบพิกเซลของช่องต่างๆ สามช่อง
int x = image.at<Vec3b>(10, 29)[0]; int y = image.at<Vec3b>(10, 29)[1]; int z = image.at<Vec3b>(10, 29)[2];
ในบรรทัดแรก เราอ่านค่าของพิกเซลซึ่งอยู่ที่ (10, 29) ของช่องสัญญาณแรก (สีน้ำเงิน) และเก็บค่าไว้ที่ตัวแปร 'x' บรรทัดที่สองและสามกำลังเก็บค่าของ 2 nd และ 3 rd ช่องทางตามลำดับ ตอนนี้ มาเรียนรู้วิธีอ่านค่าพิกเซลโดยใช้วิธี "การเข้าถึงโดยตรง"
โปรแกรมต่อไปนี้อ่านค่าพิกเซลที่ (10, 29) โดยตรง -
ตัวอย่าง
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat_<Vec3b>image;//taking an image matrix//
image = imread("sky.jpg");//loading an image//
Vec3b x = image(10, 29);//getting the pixel values//
cout << x << endl;//showing the pixel values//
system("pause");//pause the system to visualize the result//
return 0;
} ผลลัพธ์
