Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenCVApplication.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "common.h"
- using namespace std;
- #include <math.h>
- #include <queue>
- #include <random>
- void testOpenImage()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- imshow("image", src);
- waitKey();
- }
- }
- void testOpenImagesFld()
- {
- char folderName[MAX_PATH];
- if (openFolderDlg(folderName) == 0)
- return;
- char fname[MAX_PATH];
- FileGetter fg(folderName, "bmp");
- while (fg.getNextAbsFile(fname))
- {
- Mat src;
- src = imread(fname);
- imshow(fg.getFoundFileName(), src);
- if (waitKey() == 27) //ESC pressed
- break;
- }
- }
- void testImageOpenAndSave()
- {
- Mat src, dst;
- src = imread("Images/Lena_24bits.bmp", CV_LOAD_IMAGE_COLOR); // Read the image
- if (!src.data) // Check for invalid input
- {
- printf("Could not open or find the image\n");
- return;
- }
- // Get the image resolution
- Size src_size = Size(src.cols, src.rows);
- // Display window
- const char* WIN_SRC = "Src"; //window for the source image
- namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_SRC, 0, 0);
- const char* WIN_DST = "Dst"; //window for the destination (processed) image
- namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_DST, src_size.width + 10, 0);
- cvtColor(src, dst, CV_BGR2GRAY); //converts the source image to a grayscale one
- imwrite("Images/Lena_24bits_gray.bmp", dst); //writes the destination to file
- imshow(WIN_SRC, src);
- imshow(WIN_DST, dst);
- printf("Press any key to continue ...\n");
- waitKey(0);
- }
- void testNegativeImage()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- double t = (double)getTickCount(); // Get the current time [s]
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- Mat dst = Mat(height, width, CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine cu 8 biti/pixel
- // Varianta ineficienta (lenta)
- for (int i = 0; i<height; i++)
- {
- for (int j = 0; j<width; j++)
- {
- uchar val = src.at<uchar>(i, j);
- uchar neg = MAX_PATH - val;
- dst.at<uchar>(i, j) = neg;
- }
- }
- // Get the current time again and compute the time difference [s]
- t = ((double)getTickCount() - t) / getTickFrequency();
- // Print (in the console window) the processing time in [ms]
- printf("Time = %.3f [ms]\n", t * 1000);
- imshow("input image", src);
- imshow("negative image", dst);
- waitKey();
- }
- }
- void testParcurgereSimplaDiblookStyle()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- Mat dst = src.clone();
- double t = (double)getTickCount(); // Get the current time [s]
- // the fastest approach using the “diblook style”
- uchar *lpSrc = src.data;
- uchar *lpDst = dst.data;
- int w = (int)src.step; // no dword alignment is done !!!
- for (int i = 0; i<height; i++)
- for (int j = 0; j < width; j++) {
- uchar val = lpSrc[i*w + j];
- lpDst[i*w + j] = 255 - val;
- }
- // Get the current time again and compute the time difference [s]
- t = ((double)getTickCount() - t) / getTickFrequency();
- // Print (in the console window) the processing time in [ms]
- printf("Time = %.3f [ms]\n", t * 1000);
- imshow("input image", src);
- imshow("negative image", dst);
- waitKey();
- }
- }
- void testColor2Gray()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- Mat dst = Mat(height, width, CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
- // Varianta ineficienta (lenta)
- for (int i = 0; i<height; i++)
- {
- for (int j = 0; j<width; j++)
- {
- Vec3b v3 = src.at<Vec3b>(i, j);
- uchar b = v3[0];
- uchar g = v3[1];
- uchar r = v3[2];
- dst.at<uchar>(i, j) = (r + g + b) / 3;
- }
- }
- imshow("input image", src);
- imshow("gray image", dst);
- waitKey();
- }
- }
- void changeImageGray() {
- char frame[MAX_PATH];
- while (openFileDlg(frame)) {
- Mat_<uchar> src;
- src = imread(frame, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- int grayColor = src.at<uchar>(i, j) + 20;
- if (grayColor > 255) {
- src.at<uchar>(i, j) = 255;
- }
- else {
- if (grayColor < 0)
- src.at<uchar>(i, j) = 0;
- else
- src.at<uchar>(i, j) = (uchar)grayColor;
- }
- }
- }
- imshow("grayLevelAdition", src);
- waitKey();
- }
- }
- void changeImageGrayMultiplication() {
- char frame[MAX_PATH];
- while (openFileDlg(frame)) {
- Mat_<uchar> src;
- src = imread(frame, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- int grayColor = src.at<uchar>(i, j) * 2;
- if (grayColor > 255) {
- src.at<uchar>(i, j) = 255;
- }
- else {
- if (grayColor < 0)
- src.at<uchar>(i, j) = 0;
- else
- src.at<uchar>(i, j) = (uchar)grayColor;
- }
- }
- }
- imshow("grayLevelAdition", src);
- imwrite("C:\\Users\\Public\\Pictures\\MultiplicationGray.bmp", src);
- waitKey();
- }
- }
- void create256(){
- Mat img = Mat(256, 256, CV_8UC3);
- for (int i = 0; i<256; i++)
- {
- for (int j = 0; j<256; j++)
- {
- if (i < 128 && j < 128){
- Vec3b v3 = img.at<Vec3b>(i, j);
- v3[0] = 255;
- v3[1] = 255;
- v3[2] = 255;
- img.at<Vec3b>(i, j) = v3;
- }
- if (i > 128 && j < 128){
- Vec3b v3 = img.at<Vec3b>(i, j);
- v3[0] = 0;
- v3[1] = 255;
- v3[2] = 0;
- img.at<Vec3b>(i, j) = v3;
- }
- if (i > 128 && j > 128){
- Vec3b v3 = img.at<Vec3b>(i, j);
- v3[0] = 0;
- v3[1] = 255;
- v3[2] = 255;
- img.at<Vec3b>(i, j) = v3;
- }
- if (i < 128 && j > 128){
- Vec3b v3 = img.at<Vec3b>(i, j);
- v3[0] = 0;
- v3[1] = 0;
- v3[2] = 255;
- img.at<Vec3b>(i, j) = v3;
- }
- }
- }
- imshow("image", img);
- waitKey();
- }
- void testBGR2HSV()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- // Componentele d eculoare ale modelului HSV
- Mat H = Mat(height, width, CV_8UC1);
- Mat S = Mat(height, width, CV_8UC1);
- Mat V = Mat(height, width, CV_8UC1);
- // definire pointeri la matricele (8 biti/pixeli) folosite la afisarea componentelor individuale H,S,V
- uchar* lpH = H.data;
- uchar* lpS = S.data;
- uchar* lpV = V.data;
- Mat hsvImg;
- cvtColor(src, hsvImg, CV_BGR2HSV);
- // definire pointer la matricea (24 biti/pixeli) a imaginii HSV
- uchar* hsvDataPtr = hsvImg.data;
- for (int i = 0; i<height; i++)
- {
- for (int j = 0; j<width; j++)
- {
- int hi = i*width * 3 + j * 3;
- int gi = i*width + j;
- lpH[gi] = hsvDataPtr[hi] * 510 / 360; // lpH = 0 .. 255
- lpS[gi] = hsvDataPtr[hi + 1]; // lpS = 0 .. 255
- lpV[gi] = hsvDataPtr[hi + 2]; // lpV = 0 .. 255
- }
- }
- imshow("input image", src);
- imshow("H", H);
- imshow("S", S);
- imshow("V", V);
- waitKey();
- }
- }
- void inverse(){
- Mat src(3, 3, CV_32FC1);
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++){
- src.at<float>(i, j) = i + j;
- }
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- printf("%lf", src.at<float>(i, j));
- }
- printf("\n");
- }
- printf("Matrix Inverse:\n");
- Mat inverse = src.inv();
- for (int i = 0; i < 3; i++){
- for (int j = 0; j < 3; j++){
- printf("%lf", inverse.at<float>(i, j));
- }
- printf("\n");
- }
- waitKey();
- }
- void testResize()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- Mat dst1, dst2;
- //without interpolation
- resizeImg(src, dst1, 320, false);
- //with interpolation
- resizeImg(src, dst2, 320, true);
- imshow("input image", src);
- imshow("resized image (without interpolation)", dst1);
- imshow("resized image (with interpolation)", dst2);
- waitKey();
- }
- }
- void testCanny()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src, dst, gauss;
- src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- double k = 0.4;
- int pH = 50;
- int pL = (int)k*pH;
- GaussianBlur(src, gauss, Size(5, 5), 0.8, 0.8);
- Canny(gauss, dst, pL, pH, 3);
- imshow("input image", src);
- imshow("canny", dst);
- waitKey();
- }
- }
- void testVideoSequence()
- {
- VideoCapture cap("Videos/rubic.avi"); // off-line video from file
- //VideoCapture cap(0); // live video from web cam
- if (!cap.isOpened()) {
- printf("Cannot open video capture device.\n");
- waitKey(0);
- return;
- }
- Mat edges;
- Mat frame;
- char c;
- while (cap.read(frame))
- {
- Mat grayFrame;
- cvtColor(frame, grayFrame, CV_BGR2GRAY);
- Canny(grayFrame, edges, 40, 100, 3);
- imshow("source", frame);
- imshow("gray", grayFrame);
- imshow("edges", edges);
- c = cvWaitKey(0); // waits a key press to advance to the next frame
- if (c == 27) {
- // press ESC to exit
- printf("ESC pressed - capture finished\n");
- break; //ESC pressed
- };
- }
- }
- void testSnap()
- {
- VideoCapture cap(0); // open the deafult camera (i.e. the built in web cam)
- if (!cap.isOpened()) // openenig the video device failed
- {
- printf("Cannot open video capture device.\n");
- return;
- }
- Mat frame;
- char numberStr[256];
- char fileName[256];
- // video resolution
- Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
- (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
- // Display window
- const char* WIN_SRC = "Src"; //window for the source frame
- namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_SRC, 0, 0);
- const char* WIN_DST = "Snapped"; //window for showing the snapped frame
- namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
- cvMoveWindow(WIN_DST, capS.width + 10, 0);
- char c;
- int frameNum = -1;
- int frameCount = 0;
- for (;;)
- {
- cap >> frame; // get a new frame from camera
- if (frame.empty())
- {
- printf("End of the video file\n");
- break;
- }
- ++frameNum;
- imshow(WIN_SRC, frame);
- c = cvWaitKey(10); // waits a key press to advance to the next frame
- if (c == 27) {
- // press ESC to exit
- printf("ESC pressed - capture finished");
- break; //ESC pressed
- }
- if (c == 115){ //'s' pressed - snapp the image to a file
- frameCount++;
- fileName[0] = NULL;
- sprintf(numberStr, "%d", frameCount);
- strcat(fileName, "Images/A");
- strcat(fileName, numberStr);
- strcat(fileName, ".bmp");
- bool bSuccess = imwrite(fileName, frame);
- if (!bSuccess)
- {
- printf("Error writing the snapped image\n");
- }
- else
- imshow(WIN_DST, frame);
- }
- }
- }
- void MyCallBackFunc(int event, int x, int y, int flags, void* param)
- {
- //More examples: http://opencvexamples.blogspot.com/2014/01/detect-mouse-clicks-and-moves-on-image.html
- Mat* src = (Mat*)param;
- if (event == CV_EVENT_LBUTTONDOWN)
- {
- printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
- x, y,
- (int)(*src).at<Vec3b>(y, x)[2],
- (int)(*src).at<Vec3b>(y, x)[1],
- (int)(*src).at<Vec3b>(y, x)[0]);
- }
- }
- void testMouseClick()
- {
- Mat src;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- //Create a window
- namedWindow("My Window", 1);
- //set the callback function for any mouse event
- setMouseCallback("My Window", MyCallBackFunc, &src);
- //show the image
- imshow("My Window", src);
- // Wait until user press some key
- waitKey(0);
- }
- }
- /* Histogram display function - display a histogram using bars (simlilar to L3 / PI)
- Input:
- name - destination (output) window name
- hist - pointer to the vector containing the histogram values
- hist_cols - no. of bins (elements) in the histogram = histogram image width
- hist_height - height of the histogram image
- Call example:
- showHistogram ("MyHist", hist_dir, 255, 200);
- */
- void DrawCross(Mat& img, Point p, int size, Scalar color, int thickness){
- line(img, Point(p.x - size / 2, p.y), Point(p.x + size / 2, p.y), color, thickness, 8);
- line(img, Point(p.x, p.y - size / 2), Point(p.x, p.y + size / 2), color, thickness, 8);
- }
- void findObj(int event, int x, int y, int flags, void* param){
- Mat* src = (Mat*)param;
- if (event == CV_EVENT_LBUTTONDOWN)
- {
- Vec3b pixel = (*src).at<Vec3b>(y, x);
- int xc = 0;
- int yc = 0;
- int height = (*src).rows;
- int width = (*src).cols;
- Mat dst = Mat(height, width, CV_8UC3);
- int *vertical = (int*)calloc(height, sizeof(int));
- int *horizontal = (int*)calloc(width, sizeof(int));
- int aria = 0;
- int perimeter = 0;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- if (pixel == (*src).at<Vec3b>(i, j)){
- aria += 1, xc += j, yc += i, vertical[j]++, horizontal[i]++;
- if (pixel != (*src).at<Vec3b>(i + 1, j) || pixel != (*src).at<Vec3b>(i + 1, j + 1) || pixel != (*src).at<Vec3b>(i + 1, j - 1) || pixel != (*src).at<Vec3b>(i, j + 1) || pixel != (*src).at<Vec3b>(i, j - 1) || pixel != (*src).at<Vec3b>(i - 1, j) || pixel != (*src).at<Vec3b>(i - 1, j - 1) || pixel != (*src).at<Vec3b>(i - 1, j - 1))
- dst.at<Vec3b>(i, j) = *(new Vec3b(0, 0, 0)), perimeter++;
- else
- dst.at<Vec3b>(i, j) = (*src).at<Vec3b>(i, j);
- }
- else
- dst.at<Vec3b>(i, j) = (*src).at<Vec3b>(i, j);
- xc /= aria;
- yc /= aria;
- int numa = 0;
- int num1 = 0;
- int num2 = 0;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- {
- if (pixel == (*src).at<Vec3b>(i, j))
- numa += (i - yc)*(j - xc),
- num1 += (j - xc)*(j - xc),
- num2 += (i - yc)*(i - yc);
- }
- float phi = atan2(2*(float)numa, (float)(num1 - num2)) / 2;
- int teta = phi*(180 / 3.14);
- //Draw cross
- DrawCross(dst, Point(xc, yc), 20, Scalar(255, 255, 255), 2);
- float thickness = 0;
- thickness = 4 * 3.14 *(aria / (perimeter*perimeter));
- int delta = 30; // arbitrary value
- Point P1, P2;
- P1.x = xc - delta;
- P1.y = yc - (int)(delta*atan(teta)); // teta is the elongation angle in radians
- P2.x = xc + delta;
- P2.y = yc + (int)(delta*atan(teta));
- line(dst, P1, P2, Scalar(0, 0, 0), 1, 8);
- printf("Object Area %d\n", aria);
- printf("Center of mass %d %d\n", xc, yc);
- printf("Perimeter : %d\n", perimeter);
- printf("Teta %d\n", teta);
- printf("T= %.2f \n", thickness);
- imshow("Object", dst);
- waitKey(0);
- }
- }
- void findObjectArea()
- {
- Mat src;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- //Create a window
- namedWindow("Window Area", 1);
- //set the callback function for any mouse event
- setMouseCallback("Window Area", findObj, &src);
- //show the image
- imshow("Window Area", src);
- // Wait until user press some key
- waitKey(0);
- }
- }
- void genColor()
- {
- Mat src;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- //Create a window
- namedWindow("My Window", 1);
- int width = src.cols;
- int height = src.rows;
- //generare paleta de culori
- Scalar colorLUT[1000] = { 0 };
- Scalar color;
- for (int i = 1; i < 1000; i++)
- {
- Scalar color(rand() & 255, rand() & 255, rand() & 255);
- colorLUT[i] = color;
- }
- colorLUT[0] = Scalar(255, 255, 255); // fundalul va fi alb
- Mat labels = Mat::zeros(src.size(), CV_16SC1); //matricea de etichete
- Mat dst = Mat::zeros(src.size(), CV_8UC3); //matricea destinatie pt. afisare
- //printf("%d %d", labels.rows, labels.cols);
- //Procesare (labeling) ….
- int label = 0;
- for (int i = 1; i < height - 1; i++)
- for (int j = 1; j < width - 1; j++)
- {
- if (src.at<uchar>(i, j) == 0 && labels.at<short>(i, j) == 0)
- {
- label++;
- short newLabel = label % 1000;
- labels.at<short>(i, j) = newLabel;
- queue<Point2i> Q;
- Q.push({ i, j });
- while (!Q.empty())
- {
- Point2i q = Q.front();
- Q.pop();
- for (int k = -1; k <= 1; k++)
- for (int m = -1; m <= 1; m++)
- {
- int x = q.x + k;
- int y = q.y + m;
- if (src.at<uchar>(x, y) == 0 && labels.at<short>(x, y) == 0)
- {
- labels.at<short>(x, y) = newLabel;
- Q.push({ x, y });
- }
- }
- }
- }
- }
- for (int i = 1; i < height - 1; i++)
- for (int j = 1; j < width - 1; j++)
- {
- Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
- dst.at<Vec3b>(i, j)[0] = (uchar)color[0];
- dst.at<Vec3b>(i, j)[1] = (uchar)color[1];
- dst.at<Vec3b>(i, j)[2] = (uchar)color[2];
- }
- printf("%d", label);
- //show the image
- imshow("My Window", src);
- imshow("Label photo", dst);
- // Wait until user press some key
- waitKey(0);
- }
- }
- void clickObject(){
- char fname[MAX_PATH];
- while (openFileDlg(fname)){
- Mat src = imread(fname);
- namedWindow("My Window", 1);
- setMouseCallback("My Window", findObj, &src);
- imshow("My Window", src);
- waitKey();
- }
- }
- void convertRGBToGrayScale()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- Mat dst = Mat(height, width, CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
- // Varianta ineficienta (lenta)
- for (int i = 0; i<height; i++)
- for (int j = 0; j<width; j++)
- {
- Vec3b v3 = src.at<Vec3b>(i, j);
- uchar b = v3[0];
- uchar g = v3[1];
- uchar r = v3[2];
- dst.at<uchar>(i, j) = (r + g + b) / 3;
- }
- imshow("input image", src);
- imshow("converted image", dst);
- waitKey();
- }
- }
- void convertGrayScaleToBlackAndWhite()
- {
- int n;
- printf("Please enter a number: \n");
- scanf("%d", &n);
- if (!(n <= 255 && n >= 0))
- {
- printf("Wrong number!");
- return;
- }
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- Mat dst = Mat(height, width, CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
- // Varianta ineficienta (lenta)
- for (int i = 0; i<height; i++)
- for (int j = 0; j<width; j++)
- {
- uchar pixel = src.at<uchar>(i, j);
- if (pixel >= n)
- dst.at<uchar>(i, j) = 255;
- else
- dst.at<uchar>(i, j) = 0;
- }
- imshow("input image", src);
- imshow("converted image", dst);
- waitKey();
- }
- }
- float my_max(float a, float b)
- {
- return (a > b ? a : b);
- }
- float my_min(float a, float b)
- {
- return a < b ? a : b;
- }
- void convertRGBToHSV()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- Mat dst = Mat(height, width, CV_8UC3);
- Mat h_mat = Mat(height, width, CV_8UC1);
- Mat s_mat = Mat(height, width, CV_8UC1);
- Mat v_mat = Mat(height, width, CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
- // Varianta ineficienta (lenta)
- for (int i = 0; i<height; i++)
- for (int j = 0; j<width; j++)
- {
- Vec3b v3 = src.at<Vec3b>(i, j);
- float b = ((float)v3[0]) / 255;
- float g = ((float)v3[1]) / 255;
- float r = ((float)v3[2]) / 255;
- float M = my_max(my_max(r, g), b);
- float m = my_min(my_min(r, g), b);
- float C = M - m;
- //Value
- dst.at<Vec3b>(i, j)[2] = M * 255;
- v_mat.at<uchar>(i, j) = M * 255;
- //Saturation
- if (M != 0)
- {
- dst.at<Vec3b>(i, j)[1] = (C / M) * 255;
- s_mat.at<uchar>(i, j) = (C / M) * 255;
- }
- else
- {
- dst.at<Vec3b>(i, j)[1] = 0;
- s_mat.at<uchar>(i, j) = 0;
- }
- //Hue
- float H;
- if (C != 0) {
- if (M == r) H = 60 * (g - b) / C;
- if (M == g) H = 120 + 60 * (b - r) / C;
- if (M == b) H = 240 + 60 * (r - g) / C;
- }
- else // grayscale
- H = 0;
- if (H < 0)
- H = H + 360;
- {
- dst.at<Vec3b>(i, j)[0] = H * 255 / 360;
- h_mat.at<uchar>(i, j) = H * 255 / 360;
- }
- }
- imshow("input image", src);
- imshow("HSV image", dst);
- imshow("H image", h_mat);
- imshow("S image", s_mat);
- imshow("V image", v_mat);
- waitKey();
- }
- }
- void showHistogram(const std::string& name, int* hist, const int hist_cols, const int hist_height)
- {
- Mat imgHist(hist_height, hist_cols, CV_8UC3, CV_RGB(255, 255, 255)); // constructs a white image
- //computes histogram maximum
- int max_hist = 0;
- for (int i = 0; i<hist_cols; i++)
- if (hist[i] > max_hist)
- max_hist = hist[i];
- double scale = 1.0;
- scale = (double)hist_height / max_hist;
- int baseline = hist_height - 1;
- for (int x = 0; x < hist_cols; x++) {
- Point p1 = Point(x, baseline);
- Point p2 = Point(x, baseline - cvRound(hist[x] * scale));
- line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
- }
- imshow(name, imgHist);
- }
- void HistogramCalc(){
- int h[256] = { 0 };
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- {
- int g = (int) src.at<uchar>(i, j);
- h[g]++;
- }
- imshow("input image", src);
- showHistogram("Histogram", h, 256, 150);
- waitKey();
- }
- }
- void FDP(){
- int h[256] = { 0 };
- float p[256] = { 0.0 };
- int hf[256] = { 0 };
- int vec[256] = { 0 };
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname);
- int height = src.rows;
- int width = src.cols;
- int M = height*width;
- for (int i = 0; i < height; i++)
- for (int j = 0; j < width; j++)
- {
- int g = (int)src.at<uchar>(i, j);
- h[g]++;
- }
- int wh = 5;
- float th = 0.0003;
- for (int i = wh; i < 255 - wh; i++){
- float v = 0.0;
- for (int j = -wh; j <= wh; j++)
- v += h[i + j];
- hf[i] = v / (2 * wh + 1);
- }
- for (int i = wh; i < 255 - wh; i++){
- float v = 0.0;
- for (int j = -wh; j <= wh; j++)
- v += hf[i + j];
- h[i] = v / (2 * wh + 1);
- }
- for (int i = 0; i < 255; i++)
- {
- p[i] = (float)h[i] / M;
- }
- int nr = 0;
- for (int i = wh; i < 255 - wh; i++){
- float v = 0.0;
- bool max = true;
- for (int j = i-wh; j < i+wh; j++){
- if (p[i]<p[j]){
- max = false;
- break;
- }
- v += p[i];
- }
- v = v / (2 * wh + 1);
- if (max && p[i] > (v + th)){
- vec[nr] = i;
- nr++;
- }
- }
- imshow("input image", src);
- printf("Numar varfuri: %d ", nr);
- for (int i = 0; i < nr; i++){
- printf("%d ", vec[i]);
- }
- printf("\n");
- showHistogram("Histogram", h, 256, 200);
- waitKey();
- }
- }
- bool is_Inside(Mat img, int i, int j)
- {
- int height = img.rows;
- int width = img.cols;
- //Check rows
- if (!(i >= 0 && i < height))
- return FALSE;
- //Check columns
- if (!(j >= 0 && j < width))
- return FALSE;
- return TRUE;
- }
- int main()
- {
- int op;
- do
- {
- //system("cls");
- destroyAllWindows();
- printf("Menu:\n");
- printf(" 1 - Open image\n");
- printf(" 2 - Open BMP images from folder\n");
- printf(" 3 - Image negative - diblook style\n");
- printf(" 4 - BGR->HSV\n");
- printf(" 5 - Resize image\n");
- printf(" 6 - Canny edge detection\n");
- printf(" 7 - Edges in a video sequence\n");
- printf(" 8 - Snap frame from live video\n");
- printf(" 9 - Mouse callback demo\n");
- printf(" 10 - Negative image\n");
- printf(" 11 - Gray factor aditiv \n");
- printf(" 12 - Gray factor multiplicativ \n");
- printf(" 13 - Create 256x256 \n");
- printf(" 14 - Inverse matrice 3x3 \n");
- printf(" 15 - Convert RGB to GrayScale\n");
- printf(" 16 - Convert GrayScale to Black and White\n");
- printf(" 17 - Convert RGB to HSV\n");
- printf(" 18 - Calcul Histograma\n");
- printf(" 19 - FDP\n");
- printf(" 20 - Object Click\n");
- printf(" 21 - Color object\n");
- printf(" 0 - Exit\n\n");
- printf("Option: ");
- scanf("%d", &op);
- switch (op)
- {
- case 1:
- testOpenImage();
- break;
- case 2:
- testOpenImagesFld();
- break;
- case 3:
- testParcurgereSimplaDiblookStyle(); //diblook style
- break;
- case 4:
- //testColor2Gray();
- testBGR2HSV();
- break;
- case 5:
- testResize();
- break;
- case 6:
- testCanny();
- break;
- case 7:
- testVideoSequence();
- break;
- case 8:
- testSnap();
- break;
- case 9:
- testMouseClick();
- break;
- case 10:
- testNegativeImage();
- break;
- case 11:
- changeImageGray();
- break;
- case 12:
- changeImageGrayMultiplication();
- break;
- case 13:
- create256();
- break;
- case 14:
- inverse();
- break;
- case 15:
- convertRGBToGrayScale();
- break;
- case 16:
- convertGrayScaleToBlackAndWhite();
- break;
- case 17:
- convertRGBToHSV();
- break;
- case 18:
- HistogramCalc();
- break;
- case 19:
- FDP();
- break;
- case 20:
- clickObject();
- break;
- case 21:
- genColor();
- break;
- }
- } while (op != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement