이전 방법('at' 방법)에서는 픽셀 값에 액세스하는 동안 이미지 유형을 지정해야 합니다. 'at' 방법보다 간단한 또 다른 방법이 있습니다. 직접 접근 방식이라고 합니다. 이 방법을 사용하여 픽셀 값에 액세스하려면 Mat
다음 프로그램은 OpenCV에서 직접 접근 방식을 사용하여 픽셀 값을 변경하는 방법을 보여줍니다.
예시
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;//Declaring cv namespace
using namespace std;
void direct_access(Mat_<Vec3b> &image, int n){ //Declaring the function//
for (int x = 0; x < n; x++){ //initiating a for loop//
int i = rand() % image.cols;//accessing random column//
int j = rand() % image.rows;//accessing random rows//
image(j, i) = 0;//setting the pixel values to zero//
}
}
int main() {
Mat_<Vec3b> image;//taking an image matrix//
Mat unchanged_Image;//taking another image matrix//
image = imread("sky.jpg");//loading an image//
unchanged_Image = imread("sky.jpg");//loading the same image//
namedWindow("Noisy Image");//Declaring an window//
namedWindow("Unchanged Image");//Declaring another window//
direct_access(image, 4000);//calling the direct access function//
imshow("Noisy Image", image);//showing the Noisy image
imshow("Unchanged Image", unchanged_Image);//showing the unchanged image//
waitKey(0);//wait for Keystroke//
return 0;
} 출력
