Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <opencv2/opencv.hpp>
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <math.h>
- using namespace std;
- using namespace cv;
- // calculating the conversion from RGB pixel values to the values of hue
- float hue (int countR,int countG, int countB)
- {
- float h = 0.5*((countR - countG) + (countR - countB)) / sqrt(((countR - countG)*(countR - countG)) + ((countR - countB)*(countG - countB)));
- if (countG >= countB) {
- h= acos(h);
- }
- else {
- h = 2* 3.14 - acos(h);
- }
- float hue= saturate_cast<uchar>(h);
- return hue;
- }
- // calculating the conversion from RGB pixel values to the values of saturation
- float sat (int countR,int countG, int countB)
- {
- int totalcolor = countR + countG + countB;
- float s = 0;
- if (countR == countG && countG == countB)
- {
- s = 0;
- }
- else
- {
- if(countR <= countG && countR <= countB)
- {
- s = 1 - (3 * (countR / totalcolor));
- }
- if (countG <= countR && countG <= countB)
- {
- s = 1 - (3 * (countG / totalcolor));
- }
- if (countB <= countR && countB <= countG)
- {
- s = 1 - (3 * (countB / totalcolor));
- }
- }
- float sat= saturate_cast<uchar>(s);
- return sat;
- }
- // calculating the conversion from RGB pixel values to the values of intensity
- float intns (int countR,int countG, int countB)
- {
- float i = 0;
- i = ((countR + countG + countB) / 3);
- return i;
- }
- int main()
- {
- Mat image;
- image = imread("333.jpg", 1);
- Mat hueImage=image.clone();
- Mat satImage=image.clone();
- Mat intnsImage=image.clone();
- hueImage = Mat::ones(image.rows, image.cols, CV_8UC1);
- satImage = Mat::ones(image.rows, image.cols, CV_8UC1);
- intnsImage = Mat::ones(image.rows, image.cols, CV_8UC1);
- if (image.data && !image.empty()){
- imshow("Original image", image);
- //getting RGB values from every pixel
- int countR = 0, countG = 0, countB = 0;
- for (int y = 0; y < image.rows; ++y){
- for (int x = 0; x < image.cols; ++x){
- countB = image.at<Vec3b>(y, x)[0];
- countG = image.at<Vec3b>(y, x)[1];
- countR = image.at<Vec3b>(y, x)[2];
- //normalizing RGB values ?
- //countR=countR/(countR+countG+countB);
- //countG=countG/(countR+countG+countB);
- //countB=countB/(countR+countG+countB);
- intnsImage.at<unsigned char>(y, x) = intns(countR,countG,countB);
- satImage.at<unsigned char>(y, x) = (sat(countR,countG,countB));
- hueImage.at<unsigned char>(y, x) = (hue(countR,countG,countB));
- }
- }
- //displaying images
- imshow("Hue", hueImage);
- imshow("Saturation", satImage);
- imshow("Intensity", intnsImage);
- }
- waitKey(0);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement