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"
- void testOpenImage()
- {
- char fname[MAX_PATH];
- while(openFileDlg(fname))
- {
- Mat img;
- img = imread(fname);
- imshow("image",img);
- 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 img;
- img = imread(fname);
- imshow(fg.getFoundFileName(),img);
- if (waitKey()==27) //ESC pressed
- break;
- }
- }
- void testImageOpenAndSave()
- {
- Mat img, dst;
- img = imread("Images/Lena_24bits.bmp", CV_LOAD_IMAGE_COLOR); // Read the image
- if (!img.data) // Check for invalid input
- {
- printf("Could not open or find the image\n");
- return;
- }
- // Get the image resolution
- Size img_size = Size(img.cols, img.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, img_size.width + 10, 0);
- cvtColor(img, 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, img);
- 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 img = imread(fname,CV_LOAD_IMAGE_GRAYSCALE);
- int height = img.rows;
- int width = img.cols;
- Mat dst = Mat(height,width,CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine cu 8 biti/pixel
- // val ineficienta (lenta)
- for (int i=0; i<height; i++)
- {
- for (int j=0; j<width; j++)
- {
- uchar val = img.at<uchar>(i,j);
- uchar neg = 255 - 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",img);
- imshow("negative image",dst);
- waitKey();
- }
- }
- void testParcurgereSimplaDiblookStyle()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = img.rows;
- int width = img.cols;
- Mat dst = img.clone();
- double t = (double)getTickCount(); // Get the current time [s]
- // the fastest approach using the “diblook style”
- uchar *lpSrc = img.data;
- uchar *lpDst = dst.data;
- int w = (int) img.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",img);
- imshow("negative image",dst);
- waitKey();
- }
- }
- void testColor2Gray()
- {
- char fname[MAX_PATH];
- while(openFileDlg(fname))
- {
- Mat img = imread(fname);
- int height = img.rows;
- int width = img.cols;
- Mat dst = Mat(height,width,CV_8UC1);
- // Asa se acceseaaza pixelii individuali pt. o imagine RGB 24 biti/pixel
- // val ineficienta (lenta)
- for (int i=0; i<height; i++)
- {
- for (int j=0; j<width; j++)
- {
- Vec3b v3 = img.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",img);
- imshow("gray image",dst);
- waitKey();
- }
- }
- void testBGR2HSV()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat img = imread(fname);
- int height = img.rows;
- int width = img.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(img, 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", img);
- imshow("H", H);
- imshow("S", S);
- imshow("V", V);
- waitKey();
- }
- }
- void testResize()
- {
- char fname[MAX_PATH];
- while(openFileDlg(fname))
- {
- Mat img;
- img = imread(fname);
- Mat dst1,dst2;
- //without interpolation
- resizeImg(img,dst1,320,false);
- //with interpolation
- resizeImg(img,dst2,320,true);
- imshow("input image",img);
- imshow("resized image (without interpolation)",dst1);
- imshow("resized image (with interpolation)",dst2);
- waitKey();
- }
- }
- void testCanny()
- {
- char fname[MAX_PATH];
- while(openFileDlg(fname))
- {
- Mat img,dst,gauss;
- img = imread(fname,CV_LOAD_IMAGE_GRAYSCALE);
- double k = 0.4;
- int pH = 50;
- int pL = (int) k*pH;
- GaussianBlur(img, gauss, Size(5, 5), 0.8, 0.8);
- Canny(gauss,dst,pL,pH,3);
- imshow("input image",img);
- 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* img = (Mat*)param;
- if (event == CV_EVENT_LBUTTONDOWN)
- {
- printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
- x, y,
- (int)(*img).at<Vec3b>(y, x)[2],
- (int)(*img).at<Vec3b>(y, x)[1],
- (int)(*img).at<Vec3b>(y, x)[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 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 copy()
- {
- Mat img = imread("Images/flowers_24bits.bmp",CV_LOAD_IMAGE_COLOR);
- Mat img1(img.rows,img.cols,CV_8UC3);
- Mat img2(img.rows, img.cols, CV_8UC3);
- Mat img3(img.rows, img.cols, CV_8UC3);
- for (int i = 0; i < img.rows; i++){
- for (int j = 0; j < img.cols; j++){
- Vec3b pixel = img.at< Vec3b>(i, j);
- unsigned char B = pixel[0];
- unsigned char G = pixel[1];
- unsigned char R = pixel[2];
- img1.at<Vec3b>(i, j)[0] = B;
- img1.at<Vec3b>(i, j)[1] = 0;
- img1.at<Vec3b>(i, j)[2] = 0;
- img2.at<Vec3b>(i, j)[0] = 0;
- img2.at<Vec3b>(i, j)[1] = G;
- img2.at<Vec3b>(i, j)[2] = 0;
- img3.at<Vec3b>(i, j)[0] = 0;
- img3.at<Vec3b>(i, j)[1] = 0;
- img3.at<Vec3b>(i, j)[2] = R;
- }
- }
- imshow("new image", img1);
- waitKey(0);
- imshow("new image", img2);
- waitKey(0);
- imshow("new image", img3);
- waitKey(0);
- }
- void color2gray()
- {
- Mat img = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_COLOR);
- Mat img1(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i < img.rows; i++){
- for (int j = 0; j < img.cols; j++){
- Vec3b pixel = img.at< Vec3b>(i, j);
- unsigned char B = pixel[0];
- unsigned char G = pixel[1];
- unsigned char R = pixel[2];
- img1.at<uchar>(i, j) = (B + G + R) / 3;
- }
- }
- imshow("new image", img1);
- waitKey(0);
- }
- void gray2Binar(int prag)
- {
- Mat img = imread("Images/cameraman.bmp", CV_LOAD_IMAGE_GRAYSCALE);
- Mat img1(img.rows, img.cols, CV_8UC1);
- for (int i = 0; i < img.rows; i++){
- for (int j = 0; j < img.cols; j++){
- uchar pixel = img.at<uchar>(i, j);
- if (pixel < prag)
- img1.at<uchar>(i, j) = 0;
- else
- img1.at<uchar>(i, j) = 255;
- }
- }
- imshow("new image", img1);
- waitKey(0);
- }
- void rgb2Hsv()
- {
- Mat img = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_COLOR);
- Mat imgH(img.rows, img.cols, CV_8UC1);
- Mat imgS(img.rows, img.cols, CV_8UC1);
- Mat imgV(img.rows, img.cols, CV_8UC1);
- float r, g, b;
- float M, m, C;
- for (int i = 0; i < img.rows; i++){
- for (int j = 0; j < img.cols; j++){
- r = (float)img.at<Vec3b>(i,j)[2] / 255;
- g = (float)img.at<Vec3b>(i, j)[1] / 255;
- b = (float)img.at<Vec3b>(i, j)[0] / 255;
- M = max(r, max(g,b) );
- m = min(r, min(g, b));
- C = M - m;
- float V = M;
- float S;
- if (V != 0)
- S = C / V;
- else
- S = 0;
- 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
- H = 0;
- if (H < 0)
- H = H + 360;
- imgH.at<uchar>(i, j) = (uchar)(H * 255 / 360);
- imgS.at<uchar>(i, j) = (uchar)(S * 255);
- imgV.at<uchar>(i, j) = (uchar)(V * 255);
- }
- }
- imshow("new image H", imgH);
- waitKey(0);
- imshow("new image S", imgS);
- waitKey(0);
- imshow("new image V", imgV);
- waitKey(0);
- }
- void test()
- {
- Mat bgr = imread("Images/flowers_24bits.bmp", CV_LOAD_IMAGE_COLOR);
- Mat hsv;
- cvtColor(bgr, hsv, CV_BGR2HSV);
- Mat channels[3];
- split(hsv, channels);
- Mat H, S, V;
- H = channels[0] * 255 / 180;
- S = channels[1];
- V = channels[2];
- imshow("Original", bgr);
- waitKey(0);
- imshow("H", H);
- waitKey(0);
- imshow("S", S);
- waitKey(0);
- imshow("V", V);
- waitKey(0);
- }
- bool isInside(int i, int j)
- {
- Mat img = imread("Images/Lena_24bits.bmp", CV_LOAD_IMAGE_COLOR);
- if (i < img.rows && j < img.cols)
- {
- return true;
- printf("Da");
- }
- return false;
- printf("Nu");
- }
- ///lab3
- bool isInList(int x, int* list, int dimList)
- {
- for (int i = 0; i <= dimList; i++)
- {
- if (x == list[i])
- {
- return true;
- }
- }
- return false;
- }
- void showHistogramTest(const std::string& name, int* maxlist, int maxi,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));
- if (isInList(x,maxlist,maxi))
- line(imgHist, p1, p2, CV_RGB(255, 255, 0)); // histogram bins colored in magenta
- else
- line(imgHist, p1, p2, CV_RGB(255, 0, 255)); // histogram bins colored in magenta
- }
- imshow(name, imgHist);
- }
- void calcHistograma()
- {
- int hist[256] = { 0 };
- Mat img = imread("Images/alumgrns.bmp", CV_LOAD_IMAGE_GRAYSCALE);
- for (int i = 0; i < img.rows; i++){
- for (int j = 0; j < img.cols; j++){
- uchar pixel = img.at<uchar>(i, j);
- hist[pixel]++;
- }
- }
- String fereastra = "Histograma originala";
- showHistogram(fereastra, hist, 255, 255);
- waitKey();
- int k = 5;
- int hf[256] = { 0 };
- for (int i = k; i < 256-k; i++)
- {
- float avg = 0.0;
- for (int j = -k; j <= k; j++)
- avg += hist[i + j];
- avg = avg / (2 * k + 1);
- hf[i] = avg;
- }
- for (int i = k; i < 256 - k; i++)
- {
- float avg = 0.0;
- for (int j = -k; j <= k; j++)
- avg += hf[i + j];
- avg = avg / (2 * k + 1);
- hist[i] = avg;
- }
- showHistogram("hist1", hist, 255, 255);
- waitKey();
- float p[256] = {0};
- for (int g = 0; g <= 256; g++)
- {
- int M = img.rows*img.cols;
- p[g] = (float)hist[g] / M;
- }
- int wh = 8;
- float th = 0.0003;
- int maxlist[255] = {0};
- maxlist[0] = 0;
- int maxi = 1;
- for (int i = wh; i <= 256 - wh; i++)
- {
- float avg = 0.0; bool isMax = true;
- for (int j = -wh; j <= wh; j++)
- {
- avg += p[i + j];
- if (p[i + j] > p[i])
- isMax = false;
- }
- avg = (float)avg / (2 * wh + 1);
- if (isMax && p[i] > avg + th)
- {
- maxlist[maxi] = i;
- printf("%d ", maxlist[maxi]);
- maxi++;
- }
- }
- maxlist[maxi] = 255;
- showHistogramTest("hist2", maxlist, maxi,hist, 255,255);
- waitKey();
- }
- ///lab5
- void afisare(Mat img, Mat labels, int label){
- 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 dst = Mat::zeros(img.size(), CV_8UC3); //matricea destinatie pt. afisare
- for (int i = 1; i < img.rows - 1; i++)
- for (int j = 1; j < img.cols - 1; j++)
- {
- Scalar color = colorLUT[labels.at<short>(i, j)]; // valabil pt. Met. 1 BFS
- dst.at<Vec3b>(i, j)[0] = color[0];
- dst.at<Vec3b>(i, j)[1] = color[1];
- dst.at<Vec3b>(i, j)[2] = color[2];
- }
- imshow("imagine", dst);
- waitKey();
- }
- void alg_bfs()
- {
- int label = 0;
- Mat img = imread("Images/letters.bmp", CV_LOAD_IMAGE_GRAYSCALE);
- imshow("original", img);
- Mat labels = Mat::zeros(img.size(), CV_16SC1);
- for (int i = 1; i < img.rows -1 ; i++)
- {
- for (int j = 1; j < img.cols - 1 ; j++)
- {
- if (img.at<uchar>(i,j) == 0 && labels.at<short>(i, j) == 0)
- {
- label++;
- queue<Point> Q;
- labels.at<short>(i, j) = label;
- Q.push(Point(i, j));
- while (!Q.empty())
- {
- Point q = Q.front();
- Q.pop();
- uchar vecini[8];
- int di[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
- int dj[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
- for (int k = 0; k < 8; k++)
- vecini[k] = img.at<uchar>(q.x + di[k], q.y + dj[k]);
- for (int k = 0; k < 8; k++){
- if ((vecini[k] == 0) && (labels.at<short>(q.x + di[k], q.y + dj[k]) == 0))
- {
- labels.at<short>(q.x + di[k], q.y + dj[k]) = label;
- Q.push(Point(q.x + di[k], q.y + dj[k]));
- }
- }
- }
- }
- }
- }
- afisare(img, labels, label);
- }
- //laborator4
- 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 calculareProprietati(int event, int x, int y, int flags, void* param)
- {
- Mat* img = (Mat*)param;
- Mat dst;
- dst = (*img).clone();
- Mat dst1;
- dst1 = Mat::zeros((*img).size(), CV_8UC3);
- Vec3b color;
- if (event == CV_EVENT_LBUTTONDBLCLK)
- {
- printf("Pos(x,y): %d,%d Color(RGB): %d,%d,%d\n",
- x, y,
- (int)(*img).at<Vec3b>(y, x)[2],
- (int)(*img).at<Vec3b>(y, x)[1],
- (int)(*img).at<Vec3b>(y, x)[0]);
- color = img->at<Vec3b>(y, x);
- // aria
- int area = 0;
- for (int i = 0; i < (*img).rows; i++)
- for (int j = 0; j < (*img).cols; j++)
- {
- if ((*img).at<Vec3b>(i, j) == color)
- area++;
- }
- //centru de masa
- int sumRows = 0;
- int sumCols = 0;
- int cX, cY;
- for (int i = 0; i < (*img).rows; i++)
- for (int j = 0; j < (*img).cols; j++) {
- if ((*img).at<Vec3b>(i, j) == color)
- {
- sumRows += i;
- sumCols += j;
- }
- }
- cY = sumRows / area;
- cX = sumCols / area;
- DrawCross(dst, Point(cX, cY), 20, Scalar(255, 255, 255), 2);
- //axe de alungire
- int numarator = 0, numitor = 0;
- for (int i = 0; i < (*img).rows; i++)
- for (int j = 0; j < (*img).cols; j++) {
- if ((*img).at<Vec3b>(i, j) == color)
- {
- numarator = numarator + (i - cY) * (j - cX);
- numitor = numitor + (j - cX) * (j - cX) - (i - cY) * (i - cY);
- }
- }
- float fi = atan2(2 * numarator, numitor) / 2;
- int delta = 30; // arbitrary value
- Point P1, P2;
- P1.x = cX - delta;
- P1.y = cY - (int)(delta*tan(fi)); // teta is the elongation angle in radians
- P2.x = cX + delta;
- P2.y = cY + (int)(delta*tan(fi));
- line(dst, P1, P2, Scalar(0, 0, 0), 1, 8);
- imshow("new window", dst);
- fi = (fi * 180) / CV_PI;
- printf("Aria: %d \n", area);
- printf("Centru de masa: (%d , %d) \n", cX, cY);
- printf("Axa de alungire :%f \n", fi);
- //proiectii
- int *v = (int *)malloc((*img).cols*sizeof(int));
- int *h = (int *)malloc((*img).rows*sizeof(int));
- for (int i = 0; i < (*img).rows; i++)
- {
- h[i] = 0;
- }
- for (int i = 0; i < (*img).cols; i++)
- {
- v[i] = 0;
- }
- for (int i = 0; i < (*img).rows; i++)
- {
- for (int j = 0; j < (*img).cols; j++)
- {
- dst1.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
- if ((*img).at<Vec3b>(i, j) == color)
- {
- v[j]++;
- h[i]++;
- }
- }
- }
- for (int i = 0; i < (*img).cols; i++)
- {
- line(dst1, Point(i, 0), Point(i, v[i]), Scalar(0, 0, 0), 1, 8);
- }
- for (int i = 0; i < (*img).rows; i++)
- {
- line(dst1, Point(0, i), Point(h[i], i), Scalar(0, 0, 0), 1, 8);
- }
- imshow("Proiectie", dst1);
- }
- }
- void testMouseClick()
- {
- Mat img;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- img = imread(fname);
- //Create a window
- namedWindow("My Window", 1);
- //set the callback function for any mouse event
- setMouseCallback("My Window", calculareProprietati, &img);
- //show the image
- imshow("My Window", img);
- // Wait until user press some key
- waitKey(0);
- }
- }
- //lab5
- typedef struct {
- int x, y;
- byte c;
- } my_point;
- void trasareContur()
- {
- int prag = 0;
- 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 = Mat(height, width, CV_8UC1);
- for (int i = 0; i<height; i++)
- {
- for (int j = 0; j<width; j++)
- {
- if (src.at<uchar>(i, j) >0)
- dst.at<uchar>(i, j) = 255;
- else
- dst.at<uchar>(i, j) = 0;
- }
- }
- eticheta:for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- dst.at<uchar>(i, j) = 255;
- }
- }
- vector <my_point> contur;
- int dj[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
- int di[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
- byte dir;
- int x_start, y_start;
- bool ok = false;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (src.at<uchar>(i, j) == 0)
- {
- x_start = j;
- y_start = i;
- break;
- ok = true;
- }
- }
- if (ok)
- break;
- }
- dir = 7;
- contur.push_back(my_point{ x_start, y_start, 7 });
- int n = 0;
- bool finished = false;
- int j = x_start, i = y_start, x, y;
- while (!finished)
- {
- if (dir % 2 == 0)
- dir = (dir + 7) % 8;
- if (dir % 2 == 1)
- dir = (dir + 6) % 8;
- for (int k = 0; k < 7; k++)
- {
- int d = (dir + k) % 8;
- x = j + dj[d];
- y = i + di[d];
- if (src.at<uchar>(y, x) == 0)
- {
- dir = d;
- contur.push_back(my_point{ x, y, dir });
- dst.at<uchar>(y, x) = 0;
- j = x; i = y;
- n++;
- break;
- }
- else
- dir = (dir + 1) % 8;
- }
- if ((n>1) && (contur[0].x == contur[n - 1].x) && (contur[1].x == contur[n].x) && (contur[0].y == contur[n - 1].y) && (contur[1].y == contur[n].y))
- finished = true;
- }
- imshow("image", src);
- imshow("new image", dst);
- waitKey();
- }
- }
- void reconstruire()
- {
- int x = 0, y = 0, N, i, j;
- int dj[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
- int di[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
- int dir;
- Mat img = imread("Images/gray_background.bmp", CV_LOAD_IMAGE_GRAYSCALE);
- int height = img.rows;
- int width = img.cols;
- Mat dst = Mat(height, width, CV_8UC1);
- FILE* fp = fopen("Images/reconstruct.txt", "rt");
- if (!fp)
- printf("Error opening the text file !\n");
- fscanf(fp, "%d", &x);
- fscanf(fp, "%d", &y);
- fscanf(fp, "%d", &N);
- printf("%d ", x);
- printf("%d ", y);
- printf("%d ", N);
- i = y;
- j = x;
- eticheta:for (int i = 0; i < height; i++){
- for (int j = 0; j < width; j++)
- {
- dst.at<uchar>(i, j) = img.at<uchar>(i, j);
- }
- }
- for (int k = 0; k < N; k++)
- {
- fscanf(fp, "%d", &dir);
- if (i<20)
- printf("%d ", dir);
- x = j + dj[dir];
- y = i + di[dir];
- dst.at<uchar>(y, x) = 0;
- j = x; i = y;
- }
- imshow("image", img);
- imshow("new image", dst);
- waitKey();
- }
- //lab7
- #define FG 0 //obiect = negru
- #define BG 255 //fond = alb
- void dilatare() {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat dst = src.clone(); //matricea destinatie
- int height = src.rows;
- int width = src.cols;
- for (int i = 1; i < height - 2; i++){
- for (int j = 1; j < width - 2; j++){
- if (src.at<uchar>(i, j) == FG){
- for (int k = -1; k <= 1; k++){
- for (int m = -1; m <= 1; m++){
- dst.at<uchar>(i + k, j + m) = FG;
- }
- }
- }
- }
- }
- imshow("src", src);
- imshow("Dilatare", dst);
- waitKey(0);
- }
- }
- void eroziune() {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat dst = src.clone(); //matricea destinatie
- int height = src.rows;
- int width = src.cols;
- for (int i = 1; i < height - 2; i++){
- for (int j = 1; j < width - 2; j++){
- if (src.at<uchar>(i, j) == FG){
- bool ok = false;
- for (int k = -1; k <= 1; k++){
- for (int m = -1; m <= 1; m++){
- if (src.at<uchar>(i + k, j + m) == BG){
- ok = true;
- }
- }
- }
- if (ok){
- dst.at<uchar>(i, j) = BG;
- }
- }
- }
- }
- imshow("src", src);
- imshow("Eroziune", dst);
- waitKey(0);
- }
- }
- Mat dilatare2(Mat src) {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat dst = src.clone(); //matricea destinatie
- int height = src.rows;
- int width = src.cols;
- for (int i = 1; i < height - 2; i++){
- for (int j = 1; j < width - 2; j++){
- if (src.at<uchar>(i, j) == FG){
- for (int k = -1; k <= 1; k++){
- for (int m = -1; m <= 1; m++){
- dst.at<uchar>(i + k, j + m) = FG;
- }
- }
- }
- }
- }
- return dst;
- }
- }
- Mat eroziune2(Mat src) {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat dst = src.clone(); //matricea destinatie
- int height = src.rows;
- int width = src.cols;
- for (int i = 1; i < height - 2; i++){
- for (int j = 1; j < width - 2; j++){
- if (src.at<uchar>(i, j) == FG){
- bool ok = false;
- for (int k = -1; k <= 1; k++){
- for (int m = -1; m <= 1; m++){
- if (src.at<uchar>(i + k, j + m) == BG){
- ok = true;
- }
- }
- }
- if (ok){
- dst.at<uchar>(i, j) = BG;
- }
- }
- }
- }
- return dst;
- }
- }
- void inchidere(){
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat temp = src.clone(); //matricea destinatie
- temp = dilatare2(src);
- Mat dst = temp.clone();
- dst = eroziune2(temp);
- imshow("src", src);
- imshow("temp", temp);
- imshow("dst", dst);
- waitKey(0);
- }
- }
- void deschidere(){
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat temp = src.clone(); //matricea destinatie
- temp = eroziune2(src);
- Mat dst = temp.clone();
- dst = dilatare2(temp);
- imshow("src", src);
- imshow("temp", temp);
- imshow("dst", dst);
- waitKey(0);
- }
- }
- void dilatarexN() {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int n;
- printf("n=");
- scanf("%d", &n);
- Mat dst = src.clone();
- Mat temp = dst.clone();
- for (int k = 1; k <= n; k++){
- temp = dilatare2(dst);
- dst = temp.clone();
- }
- imshow("src", src);
- imshow("dst", dst);
- waitKey(0);
- }
- }
- void eroziunexN() {
- char fname[MAX_PATH];
- while (openFileDlg(fname)) {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int n;
- printf("n=");
- scanf("%d", &n);
- Mat dst = src.clone();
- Mat temp = dst.clone();
- for (int k = 1; k <= n; k++){
- temp = eroziune2(dst);
- dst = temp.clone();
- }
- imshow("src", src);
- imshow("dst", dst);
- waitKey(0);
- }
- }
- //lab8
- void automatedBinary()
- {
- char fname[MAX_PATH];
- int imax, imin, i, n1, n2;
- float tk, tkm, suma1, suma2;
- bool ok = true;
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat aux = src.clone(); //scalar(255) - background alb
- int height = src.rows;
- int width = src.cols;
- int hist[256] = { 0 };
- int hist1[256] = { 0 };
- int hf[256] = { 0 };
- float fdp[256] = { 0 }, media = 0, val = 0, sigma = 0;
- int M = height*width;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- uchar val = src.at<uchar>(i, j);
- hist[val] += 1;
- }
- }
- showHistogram("histogram", hist, 256, 256);
- for (i = 0; i < 255; i++)
- {
- media += (i*hist[i] / (float)M);
- }
- printf("%f media \n", media);
- for (i = 0; i < 255; i++)
- {
- val += (i - media)*(i - media)*hist[i] / (float)M;
- }
- sigma = sqrt(val);
- printf("%f sigma\n", sigma);
- i = 0;
- do
- {
- if (hist[i]>0)
- {
- imin = i;
- ok = false;
- }
- i++;
- } while (ok);
- i = 254;
- ok = true;
- do
- {
- if (hist[i] > 0)
- {
- imax = i;
- ok = false;
- }
- i--;
- } while (ok);
- printf("%d %d imax imin \n", imax, imin);
- tkm = (imax - imin) / 2;
- printf("%f \n", tkm);
- do
- {
- tk = tkm;
- suma1 = 0;
- suma2 = 0;
- n1 = 0;
- n2 = 0;
- for (i = imin; i <= tk; i++)
- {
- suma1 += hist[i] * i;
- n1 += hist[i];
- }
- suma1 /= (float)n1;
- for (i = tk + 1; i <= imax; i++)
- {
- suma2 += hist[i] * i;
- n2 += hist[i];
- }
- suma2 /= (float)n2;
- tkm = (suma1 + suma2) / 2.0;
- }
- while (((tkm - tk) >0.001));
- printf("%f \n", tkm);
- for (int k = 0; k < src.rows; k++)
- for (int j = 0; j < src.cols; j++)
- if (src.at<uchar>(k, j) < ((int)tkm))
- aux.at<uchar>(k, j) = 0;
- else
- aux.at<uchar>(k, j) = 255;
- imshow("input image", src);
- imshow("gray image", aux);
- }
- }
- void schimbareConstrast()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- Mat aux = src.clone(); //scalar(255) - background alb
- int goutmin, goutmax;
- goutmin = 0;
- goutmax = 200;
- int vec[256];
- int gout;
- int ginmin = 1000;
- int ginmax = -1;
- for (int i = 0; i < src.rows; i++)
- {
- for (int j = 0; j < src.cols; j++)
- {
- if (src.at<uchar>(i, j) < ginmin)
- {
- ginmin = src.at<uchar>(i, j);
- }
- if (src.at<uchar>(i, j) > ginmax)
- {
- ginmax = src.at<uchar>(i, j);
- }
- }
- }
- for (int g = 0; g < 256; g++)
- {
- gout = goutmin + (g - ginmin)*(((float)goutmax - goutmin) / (ginmax - ginmin));
- vec[g] = gout;
- }
- for (int i = 0; i < src.rows; i++)
- {
- for (int j = 0; j < src.cols; j++)
- {
- aux.at<uchar>(i, j) = vec[src.at<uchar>(i, j)];
- }
- }
- imshow("imagine initiala", src);
- imshow("schimbare contrast", aux);
- waitKey(0);
- }
- }
- 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 - Color\n");
- printf(" 11 - Color2Gray\n");
- printf(" 12 - Color2Binary\n");
- printf(" 13 - RGB2HSV\n");
- printf(" 14 - Test\n");
- printf(" 15 - isInside\n");
- printf(" 16 - Histograma\n");
- printf(" 17 - Imagine\n");
- printf(" 18 - Proprietati\n");
- //laborator5
- printf(" 19 - Contur\n");
- printf(" 20 - Reconstructie\n");
- //lab7
- printf(" 21 - Dilatere\n");
- printf(" 22 - Eroziune\n");
- printf(" 23 - Inchidere\n");
- printf(" 24 - Deschidere\n");
- printf(" 25 - DilatarexN\n");
- printf(" 26 - EroziunexN\n");
- //lab8
- printf(" 27 - Media\n");
- printf(" 28 - Contrast\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:
- copy();
- break;
- case 11:
- color2gray();
- break;
- case 12:
- gray2Binar(100);
- break;
- case 13:
- rgb2Hsv();
- break;
- case 14:
- test();
- break;
- case 15:
- isInside(22, 56);
- break;
- case 16:
- calcHistograma();
- break;
- case 17:
- alg_bfs();
- break;
- case 18:
- testMouseClick();
- break;
- case 19:
- trasareContur();
- break;
- case 20:
- reconstruire();
- break;
- case 21:
- dilatare();
- break;
- case 22:
- eroziune();
- break;
- case 23:
- inchidere();
- break;
- case 24:
- deschidere();
- break;
- case 25:
- dilatarexN();
- break;
- case 26:
- eroziunexN();
- break;
- case 27:
- automatedBinary();
- break;
- case 28:
- schimbareConstrast();
- break;
- }
- }
- while (op!=0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement