Advertisemen
Before you get into the code, you should know that
rgb(255, 255, 255) = White Color
rgb(0, 0, 0) = Black Color
Ok continuing with our last tutorial, in this tutorial, we are going to add some effect on the image like some randomly scattered white dots.
or you can create new file and add it cmake and continue with this tutorial.
Ok first of all declare this function on the top of page after importing libraries.
Next create, that function with following codes.
Hopefully, you can read the comment next to some lines of code. i gets the random point in column of the image and j gets random point in row of image. if image is grey first block of if statement will run and convert that point to white, otherwise second block will and do the same thing.
Now inside main function add these lines of code.
Other than custom function calling in this part, I have explained everything in my last tutorial. when we call tickTick function, we send what image we want to add effect to and how many dots we want.
At end your full code should look like this :
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
void tickTick(cv::Mat image, int n);
int main()
{
cv::Mat img = cv::imread("binary.jpg");
tickTick(img, 5000);
// display image
cv::namedWindow("Aarlangdi");
cv::imshow("Aarlangdi",img);
cv::waitKey(0);
}
void tickTick(cv::Mat img, int n)
{
int i,j;
for (int k=0; k<n; k++)
{
i= std::rand()%img.cols; // random point in column
j= std::rand()%img.rows; // random point in row
if (img.type() == CV_8UC1) // grey image
{
img.at<uchar>(j,i)= 255;
}
else if (img.type() == CV_8UC3) // color image
{
img.at<cv::Vec3b>(j,i)[0]= 255; // r
img.at<cv::Vec3b>(j,i)[1]= 255; // g
img.at<cv::Vec3b>(j,i)[2]= 255; // b
}
}
}
Now go ahead and run the code, you will see something like this :
Advertisemen
Tidak ada komentar:
Posting Komentar