

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change Pixel values using Direct Access Method in OpenCV?
In the previous method (the 'at' method), we need to specify the image type while accessing the pixel values. There is another method which is simpler than 'at' method. It is called direct access method. To access pixel value using this method, we need to specify the Mat type such as Mat<uchar>, Mat<Vec3b>, Mat<Vec2i> and so on.
The following program demonstrates how to change Pixel Values using Direct Access Method in OpenCV.
Example
#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; }
Output
- Related Questions & Answers
- How to change Pixel Values using 'at' Method in OpenCV?
- Remote Direct Memory Access (RDMA)
- How to Change Contrast in OpenCV using C++?
- How to get the value of a specific pixel in OpenCV using C++?
- How to read the pixel value from the multichannel image in OpenCV using C++?
- Concept of Direct Memory Access (DMA)
- How to read the pixel value of a single channel image in OpenCV using C++?
- How to reduce color using Pointer Method in OpenCV?
- How to change SMB shared folder access permission using PowerShell?
- How to view the pixel values of an image using scikit-learn in Python?
- How to change the brightness of an image in OpenCV using C++?
- How to reduce the color using Iterator Method in OpenCV?
- How to access document properties using W3C DOM method?
- Setting Transparency Based on Pixel Values in Matplotlib
- How to change the color spaces of an image using Java OpenCV library?
Advertisements