diff --git a/ColorSpaces/dataAnalysis.py b/ColorSpaces/dataAnalysis.py new file mode 100644 index 0000000..d017ac5 --- /dev/null +++ b/ColorSpaces/dataAnalysis.py @@ -0,0 +1,141 @@ +#import the required packages +import matplotlib.pyplot as plt +from matplotlib.colors import LogNorm +import cv2,glob +import numpy as np + +#specify the color for which histogram is to be plotted +color = 'pieces/yellow' +# whether the plot should be on full scale or zoomed +zoom = 1 +# load all the files in the folder +files = glob.glob(color + '*.jpg') +files.sort() +# empty arrays for separating the channels for plotting +B = np.array([]) +G = np.array([]) +R = np.array([]) +H = np.array([]) +S = np.array([]) +V = np.array([]) +Y = np.array([]) +Cr = np.array([]) +Cb = np.array([]) +LL = np.array([]) +LA = np.array([]) +LB = np.array([]) + +# Data creation +# append the values from each file to the respective channel +for fi in files[:]: + # BGR + im = cv2.imread(fi) + b = im[:,:,0] + b = b.reshape(b.shape[0]*b.shape[1]) + g = im[:,:,1] + g = g.reshape(g.shape[0]*g.shape[1]) + r = im[:,:,2] + r = r.reshape(r.shape[0]*r.shape[1]) + B = np.append(B,b) + G = np.append(G,g) + R = np.append(R,r) + # HSV + hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV) + h = hsv[:,:,0] + h = h.reshape(h.shape[0]*h.shape[1]) + s = hsv[:,:,1] + s = s.reshape(s.shape[0]*s.shape[1]) + v = hsv[:,:,2] + v = v.reshape(v.shape[0]*v.shape[1]) + H = np.append(H,h) + S = np.append(S,s) + V = np.append(V,v) + # YCrCb + ycb = cv2.cvtColor(im,cv2.COLOR_BGR2YCrCb) + y = ycb[:,:,0] + y = y.reshape(y.shape[0]*y.shape[1]) + cr = ycb[:,:,1] + cr = cr.reshape(cr.shape[0]*cr.shape[1]) + cb = ycb[:,:,2] + cb = cb.reshape(cb.shape[0]*cb.shape[1]) + Y = np.append(Y,y) + Cr = np.append(Cr,cr) + Cb = np.append(Cb,cb) + # Lab + lab = cv2.cvtColor(im,cv2.COLOR_BGR2LAB) + ll = lab[:,:,0] + ll = ll.reshape(ll.shape[0]*ll.shape[1]) + la = lab[:,:,1] + la = la.reshape(la.shape[0]*la.shape[1]) + lb = lab[:,:,2] + lb = lb.reshape(lb.shape[0]*lb.shape[1]) + LL = np.append(LL,ll) + LA = np.append(LA,la) + LB = np.append(LB,lb) + + +# Plotting the histogram +nbins = 10 +plt.figure(figsize=[20,10]) +plt.subplot(2,3,1) +plt.hist2d(B, G, bins=nbins, norm=LogNorm()) +plt.xlabel('B') +plt.ylabel('G') +plt.title('RGB') +if not zoom: + plt.xlim([0,255]) + plt.ylim([0,255]) +plt.colorbar() +plt.subplot(2,3,2) +plt.hist2d(B, R, bins=nbins, norm=LogNorm()) +plt.colorbar() +plt.xlabel('B') +plt.ylabel('R') +plt.title('RGB') +if not zoom: + plt.xlim([0,255]) + plt.ylim([0,255]) +plt.subplot(2,3,3) +plt.hist2d(R, G, bins=nbins, norm=LogNorm()) +plt.colorbar() +plt.xlabel('R') +plt.ylabel('G') +plt.title('RGB') +if not zoom: + plt.xlim([0,255]) + plt.ylim([0,255]) + +plt.subplot(2,3,4) +plt.hist2d(H, S, bins=nbins, norm=LogNorm()) +plt.colorbar() +plt.xlabel('H') +plt.ylabel('S') +plt.title('HSV') +if not zoom: + plt.xlim([0,180]) + plt.ylim([0,255]) + +plt.subplot(2,3,5) +plt.hist2d(Cr, Cb, bins=nbins, norm=LogNorm()) +plt.colorbar() +plt.xlabel('Cr') +plt.ylabel('Cb') +plt.title('YCrCb') +if not zoom: + plt.xlim([0,255]) + plt.ylim([0,255]) + +plt.subplot(2,3,6) +plt.hist2d(LA, LB, bins=nbins, norm=LogNorm()) +plt.colorbar() +plt.xlabel('A') +plt.ylabel('B') +plt.title('LAB') +if not zoom: + plt.xlim([0,255]) + plt.ylim([0,255]) + plt.savefig(color + '.png',bbox_inches='tight') +else: + plt.savefig(color + '-zoom.png',bbox_inches='tight') + +plt.show() \ No newline at end of file diff --git a/ColorSpaces/images/rub00.jpg b/ColorSpaces/images/rub00.jpg new file mode 100644 index 0000000..78fb113 Binary files /dev/null and b/ColorSpaces/images/rub00.jpg differ diff --git a/ColorSpaces/images/rub01.jpg b/ColorSpaces/images/rub01.jpg new file mode 100644 index 0000000..7f768d5 Binary files /dev/null and b/ColorSpaces/images/rub01.jpg differ diff --git a/ColorSpaces/images/rub02.jpg b/ColorSpaces/images/rub02.jpg new file mode 100644 index 0000000..e48cc0a Binary files /dev/null and b/ColorSpaces/images/rub02.jpg differ diff --git a/ColorSpaces/images/rub03.jpg b/ColorSpaces/images/rub03.jpg new file mode 100644 index 0000000..104f25a Binary files /dev/null and b/ColorSpaces/images/rub03.jpg differ diff --git a/ColorSpaces/images/rub04.jpg b/ColorSpaces/images/rub04.jpg new file mode 100644 index 0000000..a46578f Binary files /dev/null and b/ColorSpaces/images/rub04.jpg differ diff --git a/ColorSpaces/images/rub05.jpg b/ColorSpaces/images/rub05.jpg new file mode 100644 index 0000000..e886491 Binary files /dev/null and b/ColorSpaces/images/rub05.jpg differ diff --git a/ColorSpaces/images/rub06.jpg b/ColorSpaces/images/rub06.jpg new file mode 100644 index 0000000..03afe5e Binary files /dev/null and b/ColorSpaces/images/rub06.jpg differ diff --git a/ColorSpaces/images/rub07.jpg b/ColorSpaces/images/rub07.jpg new file mode 100644 index 0000000..a1bb61b Binary files /dev/null and b/ColorSpaces/images/rub07.jpg differ diff --git a/ColorSpaces/images/rub08.jpg b/ColorSpaces/images/rub08.jpg new file mode 100644 index 0000000..b433271 Binary files /dev/null and b/ColorSpaces/images/rub08.jpg differ diff --git a/ColorSpaces/images/rub09.jpg b/ColorSpaces/images/rub09.jpg new file mode 100644 index 0000000..d9f657f Binary files /dev/null and b/ColorSpaces/images/rub09.jpg differ diff --git a/ColorSpaces/interactiveColorDetect.cpp b/ColorSpaces/interactiveColorDetect.cpp new file mode 100644 index 0000000..eab9abc --- /dev/null +++ b/ColorSpaces/interactiveColorDetect.cpp @@ -0,0 +1,105 @@ +#include "opencv2/opencv.hpp" +#include +using namespace cv; +using namespace std; + +//Global Variables +Mat img, placeholder; + +// Callback function for any event on he mouse +void onMouse( int event, int x, int y, int flags, void* userdata ) +{ + if( event == EVENT_MOUSEMOVE ) + { + + Vec3b bgrPixel(img.at(y, x)); + + Mat3b hsv,ycb,lab; + // Create Mat object from vector since cvtColor accepts a Mat object + Mat3b bgr (bgrPixel); + + //Convert the single pixel BGR Mat to other formats + cvtColor(bgr, ycb, COLOR_BGR2YCrCb); + cvtColor(bgr, hsv, COLOR_BGR2HSV); + cvtColor(bgr, lab, COLOR_BGR2Lab); + + //Get back the vector from Mat + Vec3b hsvPixel(hsv.at(0,0)); + Vec3b ycbPixel(ycb.at(0,0)); + Vec3b labPixel(lab.at(0,0)); + + // Create an empty placeholder for displaying the values + placeholder = Mat::zeros(img.rows,400,CV_8UC3); + + //fill the placeholder with the values of color spaces + putText(placeholder, format("BGR [%d, %d, %d]",bgrPixel[0],bgrPixel[1],bgrPixel[2]), Point(20, 70), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1); + putText(placeholder, format("HSV [%d, %d, %d]",hsvPixel[0],hsvPixel[1],hsvPixel[2]), Point(20, 140), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1); + putText(placeholder, format("YCrCb [%d, %d, %d]",ycbPixel[0],ycbPixel[1],ycbPixel[2]), Point(20, 210), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1); + putText(placeholder, format("LAB [%d, %d, %d]",labPixel[0],labPixel[1],labPixel[2]), Point(20, 280), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1); + + + Size sz1 = img.size(); + Size sz2 = placeholder.size(); + + //Combine the two results to show side by side in a single image + Mat combinedResult(sz1.height, sz1.width+sz2.width, CV_8UC3); + Mat left(combinedResult, Rect(0, 0, sz1.width, sz1.height)); + img.copyTo(left); + Mat right(combinedResult, Rect(sz1.width, 0, sz2.width, sz2.height)); + placeholder.copyTo(right); + imshow("PRESS P for Previous, N for Next Image", combinedResult); + } +} + +int main( int argc, const char** argv ) +{ + // filename + // Read the input image + int image_number = 0; + int nImages = 10; + + if(argc > 1) + nImages = atoi(argv[1]); + + char filename[20]; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + img = imread(filename); + // Resize the image to 400x400 + Size rsize(400,400); + resize(img,img,rsize); + + if(img.empty()) + { + return -1; + } + + // Create an empty window + namedWindow("PRESS P for Previous, N for Next Image", WINDOW_AUTOSIZE); + // Create a callback function for any event on the mouse + setMouseCallback( "PRESS P for Previous, N for Next Image", onMouse ); + + imshow( "PRESS P for Previous, N for Next Image", img ); + while(1) + { + char k = waitKey(1) & 0xFF; + if (k == 27) + break; + //Check next image in the folder + if (k =='n') + { + image_number++; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + img = imread(filename); + resize(img,img,rsize); + } + //Check previous image in he folder + else if (k =='p') + { + image_number--; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + img = imread(filename); + resize(img,img,rsize); + } + } + return 0; +} \ No newline at end of file diff --git a/ColorSpaces/interactiveColorDetect.py b/ColorSpaces/interactiveColorDetect.py new file mode 100644 index 0000000..47f6585 --- /dev/null +++ b/ColorSpaces/interactiveColorDetect.py @@ -0,0 +1,65 @@ +import cv2,argparse,glob +import numpy as np + +# mouse callback function +def showPixelValue(event,x,y,flags,param): + global img, combinedResult, placeholder + + if event == cv2.EVENT_MOUSEMOVE: + # get the value of pixel from the location of mouse in (x,y) + bgr = img[y,x] + + # Convert the BGR pixel into other colro formats + ycb = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2YCrCb)[0][0] + lab = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2Lab)[0][0] + hsv = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2HSV)[0][0] + + # Create an empty placeholder for displaying the values + placeholder = np.zeros((img.shape[0],400,3),dtype=np.uint8) + + # fill the placeholder with the values of color spaces + cv2.putText(placeholder, "BGR {}".format(bgr), (20, 70), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA) + cv2.putText(placeholder, "HSV {}".format(hsv), (20, 140), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA) + cv2.putText(placeholder, "YCrCb {}".format(ycb), (20, 210), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA) + cv2.putText(placeholder, "LAB {}".format(lab), (20, 280), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA) + + # Combine the two results to show side by side in a single image + combinedResult = np.hstack([img,placeholder]) + + cv2.imshow('PRESS P for Previous, N for Next Image',combinedResult) + + +if __name__ == '__main__' : + + # load the image and setup the mouse callback function + global img + files = glob.glob('images/rub*.jpg') + files.sort() + img = cv2.imread(files[0]) + img = cv2.resize(img,(400,400)) + cv2.imshow('PRESS P for Previous, N for Next Image',img) + + # Create an empty window + cv2.namedWindow('PRESS P for Previous, N for Next Image') + # Create a callback function for any event on the mouse + cv2.setMouseCallback('PRESS P for Previous, N for Next Image',showPixelValue) + i = 0 + while(1): + k = cv2.waitKey(1) & 0xFF + # check next image in the folder + if k == ord('n'): + i += 1 + img = cv2.imread(files[i%len(files)]) + img = cv2.resize(img,(400,400)) + cv2.imshow('PRESS P for Previous, N for Next Image',img) + + # check previous image in folder + elif k == ord('p'): + i -= 1 + img = cv2.imread(files[i%len(files)]) + img = cv2.resize(img,(400,400)) + cv2.imshow('PRESS P for Previous, N for Next Image',img) + + elif k == 27: + cv2.destroyAllWindows() + break \ No newline at end of file diff --git a/ColorSpaces/interactiveColorSegment.cpp b/ColorSpaces/interactiveColorSegment.cpp new file mode 100644 index 0000000..bdf0e45 --- /dev/null +++ b/ColorSpaces/interactiveColorSegment.cpp @@ -0,0 +1,224 @@ +#include "opencv2/opencv.hpp" +#include +#include + +using namespace cv; +using namespace std; +// global variable to keep track of +bool show = false; + + +// Create a callback for event on trackbars +void onTrackbarActivity(int pos, void* userdata){ + // Just uodate the global variable that there is an event + show = true; + return; +} + + +int main(int argc, char **argv) +{ + int image_number = 0; + int nImages = 10; + if(argc > 1) + nImages = atoi(argv[1]); + char filename[20]; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + + Mat original = imread(filename); + + // image resize width and height + int resizeHeight = 250; + int resizeWidth = 250; + Size rsize(resizeHeight,resizeWidth); + resize(original, original, rsize); + + // position on the screen where the windows start + int initialX = 50; + int initialY = 50; + + // creating windows to display images + namedWindow("P-> Previous, N-> Next", WINDOW_AUTOSIZE); + namedWindow("SelectBGR", WINDOW_AUTOSIZE); + namedWindow("SelectHSV", WINDOW_AUTOSIZE); + namedWindow("SelectYCB", WINDOW_AUTOSIZE); + namedWindow("SelectLAB", WINDOW_AUTOSIZE); + + // moving the windows to stack them horizontally + moveWindow("P-> Previous, N-> Next", initialX, initialY); + moveWindow("SelectBGR", initialX + 1 * (resizeWidth + 5), initialY); + moveWindow("SelectHSV", initialX + 2 * (resizeWidth + 5), initialY); + moveWindow("SelectYCB", initialX + 3 * (resizeWidth + 5), initialY); + moveWindow("SelectLAB", initialX + 4 * (resizeWidth + 5), initialY); + + // creating trackbars to get values for YCrCb + createTrackbar("CrMin", "SelectYCB", 0, 255, onTrackbarActivity); + createTrackbar("CrMax", "SelectYCB", 0, 255, onTrackbarActivity); + createTrackbar("CbMin", "SelectYCB", 0, 255, onTrackbarActivity); + createTrackbar("CbMax", "SelectYCB", 0, 255, onTrackbarActivity); + createTrackbar("YMin", "SelectYCB", 0, 255, onTrackbarActivity); + createTrackbar("YMax", "SelectYCB", 0, 255, onTrackbarActivity); + + // creating trackbars to get values for HSV + createTrackbar("HMin", "SelectHSV", 0, 180, onTrackbarActivity); + createTrackbar("HMax", "SelectHSV", 0, 180, onTrackbarActivity); + createTrackbar("SMin", "SelectHSV", 0, 255, onTrackbarActivity); + createTrackbar("SMax", "SelectHSV", 0, 255, onTrackbarActivity); + createTrackbar("VMin", "SelectHSV", 0, 255, onTrackbarActivity); + createTrackbar("VMax", "SelectHSV", 0, 255, onTrackbarActivity); + + // creating trackbars to get values for BGR + createTrackbar("BMin", "SelectBGR", 0, 255, onTrackbarActivity); + createTrackbar("BMax", "SelectBGR", 0, 255, onTrackbarActivity); + createTrackbar("GMin", "SelectBGR", 0, 255, onTrackbarActivity); + createTrackbar("GMax", "SelectBGR", 0, 255, onTrackbarActivity); + createTrackbar("RMin", "SelectBGR", 0, 255, onTrackbarActivity); + createTrackbar("RMax", "SelectBGR", 0, 255, onTrackbarActivity); + + // creating trackbars to get values for LAB + createTrackbar("LMin", "SelectLAB", 0, 255, onTrackbarActivity); + createTrackbar("LMax", "SelectLAB", 0, 255, onTrackbarActivity); + createTrackbar("AMin", "SelectLAB", 0, 255, onTrackbarActivity); + createTrackbar("AMax", "SelectLAB", 0, 255, onTrackbarActivity); + createTrackbar("BMin", "SelectLAB", 0, 255, onTrackbarActivity); + createTrackbar("BMax", "SelectLAB", 0, 255, onTrackbarActivity); + + // show all images initially + imshow("SelectHSV", original); + imshow("SelectYCB", original); + imshow("SelectLAB", original); + imshow("SelectBGR", original); + + // declare local variables + int BMin, GMin, RMin; + int BMax, GMax, RMax; + Scalar minBGR, maxBGR; + + int HMin, SMin, VMin; + int HMax, SMax, VMax; + Scalar minHSV, maxHSV; + + int LMin, aMin, bMin; + int LMax, aMax, bMax; + Scalar minLab, maxLab; + + int YMin, CrMin, CbMin; + int YMax, CrMax, CbMax; + Scalar minYCrCb, maxYCrCb; + + Mat imageBGR, imageHSV, imageLab, imageYCrCb; + Mat maskBGR, maskHSV, maskLab, maskYCrCb; + Mat resultBGR, resultHSV, resultLab, resultYCrCb; + + char k; + while (1){ + imshow("P-> Previous, N-> Next", original); + k = waitKey(1) & 0xFF; + //Check next image in the folder + if (k =='n') + { + image_number++; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + original = imread(filename); + resize(original,original,rsize); + show = true; + } + //Check previous image in he folder + else if (k =='p') + { + image_number--; + sprintf(filename,"images/rub%02d.jpg",image_number%nImages); + original = imread(filename); + resize(original,original,rsize); + show = true; + } + + // Close all windows when 'esc' key is pressed + if (k == 27){ + break; + } + + if (show) { //If there is any event on the trackbar + show = false; + + // Get values from the BGR trackbar + BMin = getTrackbarPos("BMin", "SelectBGR"); + GMin = getTrackbarPos("GMin", "SelectBGR"); + RMin = getTrackbarPos("RMin", "SelectBGR"); + + BMax = getTrackbarPos("BMax", "SelectBGR"); + GMax = getTrackbarPos("GMax", "SelectBGR"); + RMax = getTrackbarPos("RMax", "SelectBGR"); + + minBGR = Scalar(BMin, GMin, RMin); + maxBGR = Scalar(BMax, GMax, RMax); + + // Get values from the HSV trackbar + HMin = getTrackbarPos("HMin", "SelectHSV"); + SMin = getTrackbarPos("SMin", "SelectHSV"); + VMin = getTrackbarPos("VMin", "SelectHSV"); + + HMax = getTrackbarPos("HMax", "SelectHSV"); + SMax = getTrackbarPos("SMax", "SelectHSV"); + VMax = getTrackbarPos("VMax", "SelectHSV"); + + minHSV = Scalar(HMin, SMin, VMin); + maxHSV = Scalar(HMax, SMax, VMax); + + // Get values from the LAB trackbar + LMin = getTrackbarPos("LMin", "SelectLAB"); + aMin = getTrackbarPos("AMin", "SelectLAB"); + bMin = getTrackbarPos("BMin", "SelectLAB"); + + LMax = getTrackbarPos("LMax", "SelectLAB"); + aMax = getTrackbarPos("AMax", "SelectLAB"); + bMax = getTrackbarPos("BMax", "SelectLAB"); + + minLab = Scalar(LMin, aMin, bMin); + maxLab = Scalar(LMax, aMax, bMax); + + // Get values from the YCrCb trackbar + YMin = getTrackbarPos("YMin", "SelectYCB"); + CrMin = getTrackbarPos("CrMin", "SelectYCB"); + CbMin = getTrackbarPos("CbMin", "SelectYCB"); + + YMax = getTrackbarPos("YMax", "SelectYCB"); + CrMax = getTrackbarPos("CrMax", "SelectYCB"); + CbMax = getTrackbarPos("CbMax", "SelectYCB"); + + minYCrCb = Scalar(YMin, CrMin, CbMin); + maxYCrCb = Scalar(YMax, CrMax, CbMax); + + // Convert the BGR image to other color spaces + original.copyTo(imageBGR); + cvtColor(original, imageHSV, COLOR_BGR2HSV); + cvtColor(original, imageYCrCb, COLOR_BGR2YCrCb); + cvtColor(original, imageLab, COLOR_BGR2Lab); + + // Create the mask using the min and max values obtained from trackbar and apply bitwise and operation to get the results + inRange(imageBGR, minBGR, maxBGR, maskBGR); + resultBGR = Mat::zeros(original.rows, original.cols, CV_8UC3); + bitwise_and(original, original, resultBGR, maskBGR); + + inRange(imageHSV, minHSV, maxHSV, maskHSV); + resultHSV = Mat::zeros(original.rows, original.cols, CV_8UC3); + bitwise_and(original, original, resultHSV, maskHSV); + + inRange(imageYCrCb, minYCrCb, maxYCrCb, maskYCrCb); + resultYCrCb = Mat::zeros(original.rows, original.cols, CV_8UC3); + bitwise_and(original, original, resultYCrCb, maskYCrCb); + + inRange(imageLab, minLab, maxLab, maskLab); + resultLab = Mat::zeros(original.rows, original.cols, CV_8UC3); + bitwise_and(original, original, resultLab, maskLab); + + // Show the results + imshow("SelectBGR", resultBGR); + imshow("SelectYCB", resultYCrCb); + imshow("SelectLAB", resultLab); + imshow("SelectHSV", resultHSV); + } + } + destroyAllWindows(); + return 0; +} \ No newline at end of file diff --git a/ColorSpaces/interactiveColorSegment.py b/ColorSpaces/interactiveColorSegment.py new file mode 100644 index 0000000..a0df1cc --- /dev/null +++ b/ColorSpaces/interactiveColorSegment.py @@ -0,0 +1,171 @@ +import cv2,time,argparse,glob +import numpy as np + +#global variable to keep track of +show = False + +def onTrackbarActivity(x): + global show + show = True + pass + + +if __name__ == '__main__' : + + # Get the filename from the command line + files = glob.glob('images/rub*.jpg') + files.sort() + # load the image + original = cv2.imread(files[0]) + #Resize the image + rsize = 250 + original = cv2.resize(original,(rsize,rsize)) + + #position on the screen where the windows start + initialX = 50 + initialY = 50 + + #creating windows to display images + cv2.namedWindow('P-> Previous, N-> Next',cv2.WINDOW_AUTOSIZE) + cv2.namedWindow('SelectBGR',cv2.WINDOW_AUTOSIZE) + cv2.namedWindow('SelectHSV',cv2.WINDOW_AUTOSIZE) + cv2.namedWindow('SelectYCB',cv2.WINDOW_AUTOSIZE) + cv2.namedWindow('SelectLAB',cv2.WINDOW_AUTOSIZE) + + #moving the windows to stack them horizontally + cv2.moveWindow('P-> Previous, N-> Next',initialX,initialY) + cv2.moveWindow('SelectBGR',initialX + (rsize + 5),initialY) + cv2.moveWindow('SelectHSV',initialX + 2*(rsize + 5),initialY) + cv2.moveWindow('SelectYCB',initialX + 3*(rsize + 5),initialY) + cv2.moveWindow('SelectLAB',initialX + 4*(rsize + 5),initialY) + + #creating trackbars to get values for YCrCb + cv2.createTrackbar('CrMin','SelectYCB',0,255,onTrackbarActivity) + cv2.createTrackbar('CrMax','SelectYCB',0,255,onTrackbarActivity) + cv2.createTrackbar('CbMin','SelectYCB',0,255,onTrackbarActivity) + cv2.createTrackbar('CbMax','SelectYCB',0,255,onTrackbarActivity) + cv2.createTrackbar('YMin','SelectYCB',0,255,onTrackbarActivity) + cv2.createTrackbar('YMax','SelectYCB',0,255,onTrackbarActivity) + + #creating trackbars to get values for HSV + cv2.createTrackbar('HMin','SelectHSV',0,180,onTrackbarActivity) + cv2.createTrackbar('HMax','SelectHSV',0,180,onTrackbarActivity) + cv2.createTrackbar('SMin','SelectHSV',0,255,onTrackbarActivity) + cv2.createTrackbar('SMax','SelectHSV',0,255,onTrackbarActivity) + cv2.createTrackbar('VMin','SelectHSV',0,255,onTrackbarActivity) + cv2.createTrackbar('VMax','SelectHSV',0,255,onTrackbarActivity) + + #creating trackbars to get values for BGR + cv2.createTrackbar('BGRBMin','SelectBGR',0,255,onTrackbarActivity) + cv2.createTrackbar('BGRBMax','SelectBGR',0,255,onTrackbarActivity) + cv2.createTrackbar('BGRGMin','SelectBGR',0,255,onTrackbarActivity) + cv2.createTrackbar('BGRGMax','SelectBGR',0,255,onTrackbarActivity) + cv2.createTrackbar('BGRRMin','SelectBGR',0,255,onTrackbarActivity) + cv2.createTrackbar('BGRRMax','SelectBGR',0,255,onTrackbarActivity) + + #creating trackbars to get values for LAB + cv2.createTrackbar('LABLMin','SelectLAB',0,255,onTrackbarActivity) + cv2.createTrackbar('LABLMax','SelectLAB',0,255,onTrackbarActivity) + cv2.createTrackbar('LABAMin','SelectLAB',0,255,onTrackbarActivity) + cv2.createTrackbar('LABAMax','SelectLAB',0,255,onTrackbarActivity) + cv2.createTrackbar('LABBMin','SelectLAB',0,255,onTrackbarActivity) + cv2.createTrackbar('LABBMax','SelectLAB',0,255,onTrackbarActivity) + + # show all images initially + cv2.imshow('SelectHSV',original) + cv2.imshow('SelectYCB',original) + cv2.imshow('SelectLAB',original) + cv2.imshow('SelectBGR',original) + i = 0 + while(1): + + cv2.imshow('P-> Previous, N-> Next',original) + k = cv2.waitKey(1) & 0xFF + + # check next image in folder + if k == ord('n'): + i += 1 + original = cv2.imread(files[i%len(files)]) + original = cv2.resize(original,(rsize,rsize)) + show = True + + # check previous image in folder + elif k == ord('p'): + i -= 1 + original = cv2.imread(files[i%len(files)]) + original = cv2.resize(original,(rsize,rsize)) + show = True + # Close all windows when 'esc' key is pressed + elif k == 27: + break + + if show: # If there is any event on the trackbar + show = False + + # Get values from the BGR trackbar + BMin = cv2.getTrackbarPos('BGRBMin','SelectBGR') + GMin = cv2.getTrackbarPos('BGRGMin','SelectBGR') + RMin = cv2.getTrackbarPos('BGRRMin','SelectBGR') + BMax = cv2.getTrackbarPos('BGRBMax','SelectBGR') + GMax = cv2.getTrackbarPos('BGRGMax','SelectBGR') + RMax = cv2.getTrackbarPos('BGRRMax','SelectBGR') + minBGR = np.array([BMin, GMin, RMin]) + maxBGR = np.array([BMax, GMax, RMax]) + + # Get values from the HSV trackbar + HMin = cv2.getTrackbarPos('HMin','SelectHSV') + SMin = cv2.getTrackbarPos('SMin','SelectHSV') + VMin = cv2.getTrackbarPos('VMin','SelectHSV') + HMax = cv2.getTrackbarPos('HMax','SelectHSV') + SMax = cv2.getTrackbarPos('SMax','SelectHSV') + VMax = cv2.getTrackbarPos('VMax','SelectHSV') + minHSV = np.array([HMin, SMin, VMin]) + maxHSV = np.array([HMax, SMax, VMax]) + + # Get values from the LAB trackbar + LMin = cv2.getTrackbarPos('LABLMin','SelectLAB') + AMin = cv2.getTrackbarPos('LABAMin','SelectLAB') + BMin = cv2.getTrackbarPos('LABBMin','SelectLAB') + LMax = cv2.getTrackbarPos('LABLMax','SelectLAB') + AMax = cv2.getTrackbarPos('LABAMax','SelectLAB') + BMax = cv2.getTrackbarPos('LABBMax','SelectLAB') + minLAB = np.array([LMin, AMin, BMin]) + maxLAB = np.array([LMax, AMax, BMax]) + + # Get values from the YCrCb trackbar + YMin = cv2.getTrackbarPos('YMin','SelectYCB') + CrMin = cv2.getTrackbarPos('CrMin','SelectYCB') + CbMin = cv2.getTrackbarPos('CbMin','SelectYCB') + YMax = cv2.getTrackbarPos('YMax','SelectYCB') + CrMax = cv2.getTrackbarPos('CrMax','SelectYCB') + CbMax = cv2.getTrackbarPos('CbMax','SelectYCB') + minYCB = np.array([YMin, CrMin, CbMin]) + maxYCB = np.array([YMax, CrMax, CbMax]) + + # Convert the BGR image to other color spaces + imageBGR = np.copy(original) + imageHSV = cv2.cvtColor(original,cv2.COLOR_BGR2HSV) + imageYCB = cv2.cvtColor(original,cv2.COLOR_BGR2YCrCb) + imageLAB = cv2.cvtColor(original,cv2.COLOR_BGR2LAB) + + # Create the mask using the min and max values obtained from trackbar and apply bitwise and operation to get the results + maskBGR = cv2.inRange(imageBGR,minBGR,maxBGR) + resultBGR = cv2.bitwise_and(original, original, mask = maskBGR) + + maskHSV = cv2.inRange(imageHSV,minHSV,maxHSV) + resultHSV = cv2.bitwise_and(original, original, mask = maskHSV) + + maskYCB = cv2.inRange(imageYCB,minYCB,maxYCB) + resultYCB = cv2.bitwise_and(original, original, mask = maskYCB) + + maskLAB = cv2.inRange(imageLAB,minLAB,maxLAB) + resultLAB = cv2.bitwise_and(original, original, mask = maskLAB) + + # Show the results + cv2.imshow('SelectBGR',resultBGR) + cv2.imshow('SelectYCB',resultYCB) + cv2.imshow('SelectLAB',resultLAB) + cv2.imshow('SelectHSV',resultHSV) + + cv2.destroyAllWindows() + diff --git a/ColorSpaces/pieces/blue1r1.jpg b/ColorSpaces/pieces/blue1r1.jpg new file mode 100644 index 0000000..2adfd1a Binary files /dev/null and b/ColorSpaces/pieces/blue1r1.jpg differ diff --git a/ColorSpaces/pieces/blue1r2.jpg b/ColorSpaces/pieces/blue1r2.jpg new file mode 100644 index 0000000..342b5a7 Binary files /dev/null and b/ColorSpaces/pieces/blue1r2.jpg differ diff --git a/ColorSpaces/pieces/blue1r3.jpg b/ColorSpaces/pieces/blue1r3.jpg new file mode 100644 index 0000000..a5a4ad1 Binary files /dev/null and b/ColorSpaces/pieces/blue1r3.jpg differ diff --git a/ColorSpaces/pieces/blue1r4.jpg b/ColorSpaces/pieces/blue1r4.jpg new file mode 100644 index 0000000..f44e63f Binary files /dev/null and b/ColorSpaces/pieces/blue1r4.jpg differ diff --git a/ColorSpaces/pieces/blue1r5.jpg b/ColorSpaces/pieces/blue1r5.jpg new file mode 100644 index 0000000..28f9fd2 Binary files /dev/null and b/ColorSpaces/pieces/blue1r5.jpg differ diff --git a/ColorSpaces/pieces/blue1r6.jpg b/ColorSpaces/pieces/blue1r6.jpg new file mode 100644 index 0000000..f67f9be Binary files /dev/null and b/ColorSpaces/pieces/blue1r6.jpg differ diff --git a/ColorSpaces/pieces/blue1r7.jpg b/ColorSpaces/pieces/blue1r7.jpg new file mode 100644 index 0000000..629c714 Binary files /dev/null and b/ColorSpaces/pieces/blue1r7.jpg differ diff --git a/ColorSpaces/pieces/blue1r8.jpg b/ColorSpaces/pieces/blue1r8.jpg new file mode 100644 index 0000000..7bbff99 Binary files /dev/null and b/ColorSpaces/pieces/blue1r8.jpg differ diff --git a/ColorSpaces/pieces/blue2r1.jpg b/ColorSpaces/pieces/blue2r1.jpg new file mode 100644 index 0000000..0536f86 Binary files /dev/null and b/ColorSpaces/pieces/blue2r1.jpg differ diff --git a/ColorSpaces/pieces/blue2r2.jpg b/ColorSpaces/pieces/blue2r2.jpg new file mode 100644 index 0000000..4eeeb17 Binary files /dev/null and b/ColorSpaces/pieces/blue2r2.jpg differ diff --git a/ColorSpaces/pieces/blue2r3.jpg b/ColorSpaces/pieces/blue2r3.jpg new file mode 100644 index 0000000..4c7b57c Binary files /dev/null and b/ColorSpaces/pieces/blue2r3.jpg differ diff --git a/ColorSpaces/pieces/blue2r4.jpg b/ColorSpaces/pieces/blue2r4.jpg new file mode 100644 index 0000000..acd639f Binary files /dev/null and b/ColorSpaces/pieces/blue2r4.jpg differ diff --git a/ColorSpaces/pieces/blue2r5.jpg b/ColorSpaces/pieces/blue2r5.jpg new file mode 100644 index 0000000..fa5872d Binary files /dev/null and b/ColorSpaces/pieces/blue2r5.jpg differ diff --git a/ColorSpaces/pieces/blue2r6.jpg b/ColorSpaces/pieces/blue2r6.jpg new file mode 100644 index 0000000..4f99466 Binary files /dev/null and b/ColorSpaces/pieces/blue2r6.jpg differ diff --git a/ColorSpaces/pieces/blue2r7.jpg b/ColorSpaces/pieces/blue2r7.jpg new file mode 100644 index 0000000..99ebadd Binary files /dev/null and b/ColorSpaces/pieces/blue2r7.jpg differ diff --git a/ColorSpaces/pieces/blue2r8.jpg b/ColorSpaces/pieces/blue2r8.jpg new file mode 100644 index 0000000..585ae8a Binary files /dev/null and b/ColorSpaces/pieces/blue2r8.jpg differ diff --git a/ColorSpaces/pieces/greenr1.jpg b/ColorSpaces/pieces/greenr1.jpg new file mode 100644 index 0000000..9d132e7 Binary files /dev/null and b/ColorSpaces/pieces/greenr1.jpg differ diff --git a/ColorSpaces/pieces/greenr2.jpg b/ColorSpaces/pieces/greenr2.jpg new file mode 100644 index 0000000..4eb9bf2 Binary files /dev/null and b/ColorSpaces/pieces/greenr2.jpg differ diff --git a/ColorSpaces/pieces/greenr3.jpg b/ColorSpaces/pieces/greenr3.jpg new file mode 100644 index 0000000..3c23e63 Binary files /dev/null and b/ColorSpaces/pieces/greenr3.jpg differ diff --git a/ColorSpaces/pieces/greenr4.jpg b/ColorSpaces/pieces/greenr4.jpg new file mode 100644 index 0000000..609a1aa Binary files /dev/null and b/ColorSpaces/pieces/greenr4.jpg differ diff --git a/ColorSpaces/pieces/greenr5.jpg b/ColorSpaces/pieces/greenr5.jpg new file mode 100644 index 0000000..293e1d6 Binary files /dev/null and b/ColorSpaces/pieces/greenr5.jpg differ diff --git a/ColorSpaces/pieces/greenr6.jpg b/ColorSpaces/pieces/greenr6.jpg new file mode 100644 index 0000000..2dbe961 Binary files /dev/null and b/ColorSpaces/pieces/greenr6.jpg differ diff --git a/ColorSpaces/pieces/greenr7.jpg b/ColorSpaces/pieces/greenr7.jpg new file mode 100644 index 0000000..b80a2ac Binary files /dev/null and b/ColorSpaces/pieces/greenr7.jpg differ diff --git a/ColorSpaces/pieces/greenr8.jpg b/ColorSpaces/pieces/greenr8.jpg new file mode 100644 index 0000000..8b594ab Binary files /dev/null and b/ColorSpaces/pieces/greenr8.jpg differ diff --git a/ColorSpaces/pieces/orange1r1.jpg b/ColorSpaces/pieces/orange1r1.jpg new file mode 100644 index 0000000..7d4f438 Binary files /dev/null and b/ColorSpaces/pieces/orange1r1.jpg differ diff --git a/ColorSpaces/pieces/orange1r2.jpg b/ColorSpaces/pieces/orange1r2.jpg new file mode 100644 index 0000000..517df8e Binary files /dev/null and b/ColorSpaces/pieces/orange1r2.jpg differ diff --git a/ColorSpaces/pieces/orange1r3.jpg b/ColorSpaces/pieces/orange1r3.jpg new file mode 100644 index 0000000..7cedb43 Binary files /dev/null and b/ColorSpaces/pieces/orange1r3.jpg differ diff --git a/ColorSpaces/pieces/orange1r4.jpg b/ColorSpaces/pieces/orange1r4.jpg new file mode 100644 index 0000000..0849277 Binary files /dev/null and b/ColorSpaces/pieces/orange1r4.jpg differ diff --git a/ColorSpaces/pieces/orange1r5.jpg b/ColorSpaces/pieces/orange1r5.jpg new file mode 100644 index 0000000..ffb72cf Binary files /dev/null and b/ColorSpaces/pieces/orange1r5.jpg differ diff --git a/ColorSpaces/pieces/orange1r6.jpg b/ColorSpaces/pieces/orange1r6.jpg new file mode 100644 index 0000000..0bf95e6 Binary files /dev/null and b/ColorSpaces/pieces/orange1r6.jpg differ diff --git a/ColorSpaces/pieces/orange1r7.jpg b/ColorSpaces/pieces/orange1r7.jpg new file mode 100644 index 0000000..dd63ecf Binary files /dev/null and b/ColorSpaces/pieces/orange1r7.jpg differ diff --git a/ColorSpaces/pieces/orange1r8.jpg b/ColorSpaces/pieces/orange1r8.jpg new file mode 100644 index 0000000..f42ee76 Binary files /dev/null and b/ColorSpaces/pieces/orange1r8.jpg differ diff --git a/ColorSpaces/pieces/orange2r1.jpg b/ColorSpaces/pieces/orange2r1.jpg new file mode 100644 index 0000000..952c0fc Binary files /dev/null and b/ColorSpaces/pieces/orange2r1.jpg differ diff --git a/ColorSpaces/pieces/orange2r2.jpg b/ColorSpaces/pieces/orange2r2.jpg new file mode 100644 index 0000000..9567567 Binary files /dev/null and b/ColorSpaces/pieces/orange2r2.jpg differ diff --git a/ColorSpaces/pieces/orange2r3.jpg b/ColorSpaces/pieces/orange2r3.jpg new file mode 100644 index 0000000..d290e3e Binary files /dev/null and b/ColorSpaces/pieces/orange2r3.jpg differ diff --git a/ColorSpaces/pieces/orange2r4.jpg b/ColorSpaces/pieces/orange2r4.jpg new file mode 100644 index 0000000..3242ccf Binary files /dev/null and b/ColorSpaces/pieces/orange2r4.jpg differ diff --git a/ColorSpaces/pieces/orange2r5.jpg b/ColorSpaces/pieces/orange2r5.jpg new file mode 100644 index 0000000..d7374e1 Binary files /dev/null and b/ColorSpaces/pieces/orange2r5.jpg differ diff --git a/ColorSpaces/pieces/orange2r6.jpg b/ColorSpaces/pieces/orange2r6.jpg new file mode 100644 index 0000000..a43460f Binary files /dev/null and b/ColorSpaces/pieces/orange2r6.jpg differ diff --git a/ColorSpaces/pieces/orange2r7.jpg b/ColorSpaces/pieces/orange2r7.jpg new file mode 100644 index 0000000..b6e414b Binary files /dev/null and b/ColorSpaces/pieces/orange2r7.jpg differ diff --git a/ColorSpaces/pieces/orange2r8.jpg b/ColorSpaces/pieces/orange2r8.jpg new file mode 100644 index 0000000..c2e46f4 Binary files /dev/null and b/ColorSpaces/pieces/orange2r8.jpg differ diff --git a/ColorSpaces/pieces/redr1.jpg b/ColorSpaces/pieces/redr1.jpg new file mode 100644 index 0000000..a8e063a Binary files /dev/null and b/ColorSpaces/pieces/redr1.jpg differ diff --git a/ColorSpaces/pieces/redr2.jpg b/ColorSpaces/pieces/redr2.jpg new file mode 100644 index 0000000..efd2de0 Binary files /dev/null and b/ColorSpaces/pieces/redr2.jpg differ diff --git a/ColorSpaces/pieces/redr3.jpg b/ColorSpaces/pieces/redr3.jpg new file mode 100644 index 0000000..d1258f7 Binary files /dev/null and b/ColorSpaces/pieces/redr3.jpg differ diff --git a/ColorSpaces/pieces/redr4.jpg b/ColorSpaces/pieces/redr4.jpg new file mode 100644 index 0000000..9d732d3 Binary files /dev/null and b/ColorSpaces/pieces/redr4.jpg differ diff --git a/ColorSpaces/pieces/redr5.jpg b/ColorSpaces/pieces/redr5.jpg new file mode 100644 index 0000000..c2808a9 Binary files /dev/null and b/ColorSpaces/pieces/redr5.jpg differ diff --git a/ColorSpaces/pieces/redr6.jpg b/ColorSpaces/pieces/redr6.jpg new file mode 100644 index 0000000..11460c6 Binary files /dev/null and b/ColorSpaces/pieces/redr6.jpg differ diff --git a/ColorSpaces/pieces/redr7.jpg b/ColorSpaces/pieces/redr7.jpg new file mode 100644 index 0000000..47dd0d3 Binary files /dev/null and b/ColorSpaces/pieces/redr7.jpg differ diff --git a/ColorSpaces/pieces/redr8.jpg b/ColorSpaces/pieces/redr8.jpg new file mode 100644 index 0000000..85248b1 Binary files /dev/null and b/ColorSpaces/pieces/redr8.jpg differ diff --git a/ColorSpaces/pieces/yellowr1.jpg b/ColorSpaces/pieces/yellowr1.jpg new file mode 100644 index 0000000..9eb8a84 Binary files /dev/null and b/ColorSpaces/pieces/yellowr1.jpg differ diff --git a/ColorSpaces/pieces/yellowr2.jpg b/ColorSpaces/pieces/yellowr2.jpg new file mode 100644 index 0000000..d72bfb5 Binary files /dev/null and b/ColorSpaces/pieces/yellowr2.jpg differ diff --git a/ColorSpaces/pieces/yellowr3.jpg b/ColorSpaces/pieces/yellowr3.jpg new file mode 100644 index 0000000..1f8b553 Binary files /dev/null and b/ColorSpaces/pieces/yellowr3.jpg differ diff --git a/ColorSpaces/pieces/yellowr4.jpg b/ColorSpaces/pieces/yellowr4.jpg new file mode 100644 index 0000000..52bd0eb Binary files /dev/null and b/ColorSpaces/pieces/yellowr4.jpg differ diff --git a/ColorSpaces/pieces/yellowr5.jpg b/ColorSpaces/pieces/yellowr5.jpg new file mode 100644 index 0000000..87d0f67 Binary files /dev/null and b/ColorSpaces/pieces/yellowr5.jpg differ diff --git a/ColorSpaces/pieces/yellowr6.jpg b/ColorSpaces/pieces/yellowr6.jpg new file mode 100644 index 0000000..4602347 Binary files /dev/null and b/ColorSpaces/pieces/yellowr6.jpg differ diff --git a/ColorSpaces/pieces/yellowr7.jpg b/ColorSpaces/pieces/yellowr7.jpg new file mode 100644 index 0000000..c1f10e0 Binary files /dev/null and b/ColorSpaces/pieces/yellowr7.jpg differ diff --git a/ColorSpaces/pieces/yellowr8.jpg b/ColorSpaces/pieces/yellowr8.jpg new file mode 100644 index 0000000..2f0c331 Binary files /dev/null and b/ColorSpaces/pieces/yellowr8.jpg differ diff --git a/FaceSwap/faceSwap.cpp b/FaceSwap/faceSwap.cpp index a7bc4f7..6d05338 100644 --- a/FaceSwap/faceSwap.cpp +++ b/FaceSwap/faceSwap.cpp @@ -1,14 +1,14 @@ #include #include #include - +#include using namespace cv; using namespace std; //Read points from text file vector readPoints(string pointsFileName){ vector points; - ifstream ifs(pointsFileName); + ifstream ifs (pointsFileName.c_str()); float x, y; int count = 0; while(ifs >> x >> y)