OpenCV access pixel value image.at<uchar> image.at<cv::Vec3b>(j, i)

access pixel value

The cv::Mat class contains a variety of methods that can be used to access various attributes of the image: use the public member variables cols and rows to get the number of columns and rows of the image; use cv::Mat's at(int y, int x) method to access the element, where x is the column number and y is the row number. The type of the return value of the method must be clear at compile time, because cv::Mat can accept elements of any type, so the programmer needs to specify the expected type of the return value. Because of this, the at method is implemented as a template method. When calling the at method, you must specify the type of image element, for example:

       image.at<uchar>(j, i)= 255;

Each pixel of a color image corresponds to three parts: the red channel, the green channel, and the blue channel, so the cv::Mat class that contains a color image returns a vector containing three 8-bit values. OpenCV defines a type for such short vectors, cv::Vec3b. This vector contains three data of type unsigned character. So, the method to access elements in colored pixels looks like this:

        image.at<cv::Vec3b>(j, i)[channel]= value;

The channel index is used to specify one of the three color channels. The order in which OpenCV stores channel data is blue, green and red (so blue is channel 0). You can also use short vectors directly, as follows:

        image.at<cv::Vec3b>(j, i) = cv::Vec3b(255, 255, 255);

Application Case - Adding Noise

//
// Created by zhongcheng Dai on 2023/5/9.
//
#include <random>
#include <iostream>
#include "opencv2/opencv.hpp"

void salt(cv::Mat image,int n){
    
    
    //随机数生成器
    std::default_random_engine generator;
    std::uniform_int_distribution<int> randomRow(0,image.rows-1);
    std::uniform_int_distribution<int> randomCol(0,image.cols-1);

    int i,j;
    for(int k = 0; k<n;k++){
    
    
        //随机生成图形位置
        i = randomCol(generator);
        j = randomRow(generator);
        if(image.type() == CV_8UC1){
    
     //灰度图像
          image.at<uchar>(j,i) = 0;
        }else if(image.type() == CV_8UC3){
    
    
            image.at<cv::Vec3b>(j,i)[0] = 0;
            image.at<cv::Vec3b>(j,i)[1] = 0;
            image.at<cv::Vec3b>(j,i)[2] = 0;
        }
    }
}




int main(){
    
    
    cv::Mat a = cv::imread("../../a.png",1);

    cv::imshow("image",a);



    salt(a,1000);



    cv::imshow("image_salt",a);

    cv::waitKey(0);




    std::cout<<"test the main!"<<std::endl;
    return 0;
}
cmake_minimum_required(VERSION 3.24)

project(salt_and_pepper)

find_package(OpenCV 4.6)

set(CMAKE_CXX_STANDARD_REQUIRED C11)

set(CMAKE_CXX_STANDARD_REQUIRED True)

message("OpenCV: ${OpenCV_VERSION}")

add_executable(${
    
    CMAKE_PROJECT_NAME})

target_sources(${
    
    CMAKE_PROJECT_NAME} PRIVATE
        main.cpp)

target_link_directories(${
    
    CMAKE_PROJECT_NAME} PRIVATE
        ${
    
    OpenCV_INCLUDE_DIRS})

TARGET_LINK_LIBRARIES(${
    
    CMAKE_PROJECT_NAME} PRIVATE
        ${
    
    OpenCV_LIBS})

operation result:

Please add a picture description

Guess you like

Origin blog.csdn.net/m0_49302377/article/details/130584504