Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Upgrade code #12

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 68 additions & 80 deletions experiment/assignment/codes/im_colour.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,72 @@
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<opencv/cv.h>
#include<opencv/highgui.h>
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
IplImage *source=cvLoadImage(argv[1],1);
int c=cvGetSize(source).width;
int r=cvGetSize(source).height;
IplImage* out_img=cvCreateImage(cvSize(c,r),IPL_DEPTH_8U,3);
IplImage *p1=cvCreateImage(cvSize(c,r),IPL_DEPTH_8U,1);
IplImage *p2=cvCreateImage(cvSize(c,r),IPL_DEPTH_8U,1);
IplImage *p3=cvCreateImage(cvSize(c,r),IPL_DEPTH_8U,1);
char *img_nm,*img_typ;
img_nm = strtok (argv[2],".");
img_typ = strtok (NULL, ".");
if(atoi(argv[3])==1 || atoi(argv[3])==5)
{
// converting the RGB image to HSV
// cout<<"converting the RGB image to HSV"<<endl;
cvCvtColor(source,out_img,CV_RGB2HSV);
cvSplit(out_img,p1,p2,p3,0);
}
else if(atoi(argv[3])==2)
{
// converting the RGB image to CMY
// cout<<"converting the RGB image to CMY"<<endl;
CvScalar s,t;
t.val[0]=0;
t.val[1]=0;
t.val[2]=0;
for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++)
{
s=cvGet2D(source,i,j);
t.val[0]=(s.val[0]+s.val[1])/2;
cvSet2D(p1,i,j,t);
t.val[0]=(s.val[1]+s.val[2])/2;
cvSet2D(p2,i,j,t);
t.val[0]=(s.val[0]+s.val[2])/2;
cvSet2D(p3,i,j,t);
}
}
cvMerge(p1,p2,p3,NULL,out_img);
}
else if(atoi(argv[3])==3)
{
// converting the RGB image to YCrCb
// cout<<"converting the RGB image to YCrCb"<<endl;
cvCvtColor(source,out_img,CV_RGB2YCrCb);
cvSplit(out_img,p1,p2,p3,0);
}
else if(atoi(argv[3])==4)
{
cvSplit(source,p1,p2,p3,0);
}
char nam[30];
int n;
/* if(atoi(argv[3])==5) {
n = sprintf(nam,"%s4.%s",img_nm,img_typ);
cvSaveImage(nam,p1);
n = sprintf(nam,"%s5.%s",img_nm,img_typ);
cvSaveImage(nam,p2);
n = sprintf(nam,"%s6.%s",img_nm,img_typ);
cvSaveImage(nam,p3);
} else
{ */
n = sprintf(nam,"%s1.%s",img_nm,img_typ);
cvSaveImage(nam,p1);
n = sprintf(nam,"%s2.%s",img_nm,img_typ);
cvSaveImage(nam,p2);
n = sprintf(nam,"%s3.%s",img_nm,img_typ);
cvSaveImage(nam,p3);
cvReleaseImage(&p1);
cvReleaseImage(&p2);
cvReleaseImage(&p3);
cvReleaseImage(&source);
cvReleaseImage(&out_img);
return 0;
if (argc < 4)
{
cout << "Usage: " << argv[0] << " <input_image> <output_prefix> <conversion_type>" << endl;
cout << "Conversion types:" << endl;
cout << "1: RGB to HSV" << endl;
cout << "2: RGB to CMY" << endl;
cout << "3: RGB to YCrCb" << endl;
cout << "4: RGB split into individual channels" << endl;
cout << "5: RGB to separate CMY channels" << endl;
return -1;
}

Mat source = imread(argv[1]);
if (source.empty())
{
cout << "Error loading image " << argv[1] << endl;
return -1;
}

vector<Mat> channels(3);

switch (atoi(argv[3]))
{
case 1:
cvtColor(source, source, COLOR_BGR2HSV);
split(source, channels);
break;

case 2:
channels[0] = (source.col(0) + source.col(1)) / 2;
channels[1] = (source.col(1) + source.col(2)) / 2;
channels[2] = (source.col(0) + source.col(2)) / 2;
break;

case 3:
cvtColor(source, source, COLOR_BGR2YCrCb);
split(source, channels);
break;

case 4:
split(source, channels);
break;

case 5:
channels[0] = source.col(0);
channels[1] = source.col(1);
channels[2] = source.col(2);
break;

default:
cout << "Invalid conversion type" << endl;
return -1;
}

string output_prefix = argv[2];
string output_suffix[3] = { "1", "2", "3" };
for (int i = 0; i < 3; ++i)
{
string output_name = output_prefix + output_suffix[i] + ".jpg";
imwrite(output_name, channels[i]);
}

return 0;
}
Loading
Loading