- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 'at' Method in OpenCV?
In a grayscale image, the pixel value is a single numeric value. But in a color image such as RGB image, the pixel is a vector having three values. These three values represent three channels.
Here we will create a function that accesses both the grayscale image and RGB image pixel values and randomly adds noise to image pixels. Then we call the function inside the main() function to observe the result.
The following program demonstrates how to change Pixel Values using 'at' method in OpenCV.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> using namespace cv;//Declaring cv namespace using namespace std; void adding_Noise(Mat& image, int n){ //'adding_Noise' 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// if (image.channels() == 1){ //apply noise to grayscale image// image.at<uchar>(j, i) = 0;//Changing the value of pixel// } if (image.channels() == 3){ //apply noise to RGB image// image.at<Vec3b>(j, i)[0] = 0;//Changing the value of first channel// image.at<Vec3b>(j, i)[1] = 0;//Changing the value of first channel// image.at<Vec3b>(j, i)[2] = 0;//Changing the value of first channel// } } } int main() { Mat 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// adding_Noise(image, 4000);//calling the 'adding_Noise' function// imshow("Noisy Image", image);//showing the Noisy image imshow("Unchanged Image",unchanged_Image);//showing the unchanged image// waitKey(0);//wait for Keystroke// destroyAllWindows();//return all allocated memory return 0; }
Output
- Related Articles
- How to change Pixel values using Direct Access Method in OpenCV?
- How to get the value of a specific pixel in OpenCV using C++?
- How to access and modify pixel value in an image using OpenCV Python?
- How to read the pixel value from the multichannel image in OpenCV using C++?
- How to Change Contrast in OpenCV using C++?
- How to read the pixel value of a single channel image in OpenCV using C++?
- How to view the pixel values of an image using scikit-learn in Python?
- How to reduce color using Pointer Method in OpenCV?
- How to reduce the color using Iterator Method in OpenCV?
- How to Change the Time Interval of setinterval() Method at RunTime using JavaScript ?
- How to change the brightness of an image in OpenCV using C++?
- How to change the contrast and brightness of an image using OpenCV in Python?
- Setting Transparency Based on Pixel Values in Matplotlib
- How to set a single pixel using imagesetpixel() function in PHP?
- How to find the HSV values of a color using OpenCV Python?

Advertisements