Picking Apart Colours
Recently, I've been pretty busy with university classes and exams. To get my mind off the exams, I've made a simple program using OpenCV.
If you've ever had an interest in digital images are made or photography in general, you might have heard about "pixels". In essence, every image that you see on a computer screen consists of small points of colors called "pixels". All of these small points of color, when viewed as a whole, make up an image. Well, without going too much into the details, these pixels are made up of three colors; red,green and blue. Generally, each color is made up using 8 bits of memory. So, effectively, you can set each color as a value between 0 and 255. Just think of every image as a combination of red, green and blue where the value from 0 to 255 represents how much of each color there is.Knowing this, I got to thinking that it would be pretty fun to to see EXACTLY how much of red, blue and green there is in a sample image. And so I did!
So basically, the following code goes over every pixel in the image on the right, and depending on the user's choice, shows the same image with the other two colors removed. For example, if I selected blue, it removes green and red components from the image.
If you've ever had an interest in digital images are made or photography in general, you might have heard about "pixels". In essence, every image that you see on a computer screen consists of small points of colors called "pixels". All of these small points of color, when viewed as a whole, make up an image. Well, without going too much into the details, these pixels are made up of three colors; red,green and blue. Generally, each color is made up using 8 bits of memory. So, effectively, you can set each color as a value between 0 and 255. Just think of every image as a combination of red, green and blue where the value from 0 to 255 represents how much of each color there is.Knowing this, I got to thinking that it would be pretty fun to to see EXACTLY how much of red, blue and green there is in a sample image. And so I did!
So basically, the following code goes over every pixel in the image on the right, and depending on the user's choice, shows the same image with the other two colors removed. For example, if I selected blue, it removes green and red components from the image.
#include<iostream>
#include<highgui.h>
using namespace std;
using namespace cv;
//Each colour channel is in order BGR
void colourReduce(Mat &image,int opt_main)
{
char ans = 'y';
Mat back_mat;
image.copyTo(back_mat);
int rows = image.rows;
int columns = image.cols*image.channels();
while(ans == 'y')
{
back_mat.copyTo(image); //This sets the image back to its original colour
cout<<endl<<"Which colour would you like to see the image in?"<<endl;
cout<<"1.Blue"<<endl<<"2.Green"<<endl<<"3.Red"<<endl;
cin>>opt_main;
for(unsigned int i = 0;i<rows;i++)
{
uchar* stuff = image.ptr<uchar>(i); //Pointer that points to different rows of the image
if(opt_main == 1)
{
for(unsigned int j = 0;j<columns; j=j+3) //Shows all the blue in the image
{
stuff[j+1] = 0;
stuff[j+2] = 0;
}
}
else if(opt_main == 2) //Shows all the green
{
for(unsigned int j = 1;j<columns;j=j+3)
{
stuff[j-1] = 0;
stuff[j+1] = 0;
}
}
else if(opt_main == 3) //Shows all the red
{
for(unsigned int j = 2;j<columns;j=j+3)
{
stuff[j-1] = 0;
stuff[j-2] = 0;
}
}
}
namedWindow("Image",CV_WINDOW_KEEPRATIO);
imshow("Image",image);
waitKey(0);
destroyAllWindows();
cout<<"Would you like to try another colour removal?"<<endl;
cin>>ans;
}
return;
}
int main(int argc, char** argv)
{
char img_name[30];
int opt;
cout<<"Please enter the name of the image you wish to reduce"<<endl;
cin.getline(img_name,30);
Mat img_in = imread(img_name,CV_LOAD_IMAGE_ANYCOLOR);
cout<<img_name;
colourReduce(img_in,opt);
}
So,as usual, applied to a sample image. You get the following images depending on which option you select.
Comments
Post a Comment