Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
- #include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
- #include <opencv2/highgui/highgui.hpp>
- #include <stdio.h>
- #include <math.h>
- #include <iostream>
- using namespace std;
- using namespace cv;
- int prog=40;
- void detectEdges(Mat_<uchar> &peppersPixels, Mat_<uchar> &handEdgesMergedPixels, int centerX, int centerY);
- int _tmain(int argc, _TCHAR* argv[])
- {
- // Stworzenie okna w którym przechwycone obrazy będą wyświetlane
- cvNamedWindow("Wykrywanie krawędzi", CV_WINDOW_AUTOSIZE);
- // Pobranie obrazu
- Mat imagePeppers = imread("hand.jpg",CV_LOAD_IMAGE_GRAYSCALE);
- // Utworzenie obiektu przechowującego obraz z wyznaczonymi krawędziami
- Mat handEdgesMerged = imagePeppers.clone();
- // Uzyskanie macierzy pikseli na podstawie obiektów Mat
- Mat_<uchar> handPixels = imagePeppers;
- Mat_<uchar> handEdgesMergedPixels = handEdgesMerged;
- // Wyznaczenie wpółrzędnych środa obrazu
- int centerX = handPixels.cols/2;
- int centerY = handPixels.rows/2;
- // Obracanie obrazu
- detectEdges(handPixels, handEdgesMergedPixels, centerX, centerY);
- // Wyświetlanie obrazu
- imshow("Wykrywanie krawędzi", handEdgesMerged);
- // Oczekiwanie na wciśnięcie klawisza Esc lub Enter
- char key;
- do key = cvWaitKey(1);
- while(key != 27 && key != 13);
- int prog=100;
- // Niszczenie okna
- cvDestroyWindow("Wykrywanie krawędzi");
- return 0;
- }
- void detectEdges(Mat_<uchar> &handPixels, Mat_<uchar> &handEdgesMergedPixels, int centerX, int centerY)
- {
- /* Funkcja wyznaczająca krawędzie za pomocą maski morfologicznej podanej przez prowadzącego.
- Wyznaczane są najpierw krawędzie pionowe, później poziome. Następnie wykonywane jest scalenie
- krawędzi pionowych i poziomych, przeskalowanie obrazu scalonego do zakresu 0-255 oraz binaryzacja
- z eksperymentalnie dobranym progiem.
- Wynikowy obraz zostaje zapisany w handEdgesMergedPixels.*/
- // Utworzenie obiektów przechowujących obrazy z wyznaczonymi krawędziami: pionowymi i poziomymi
- Mat handEdgesVertical = handPixels.clone();
- Mat handEdgesHorizontal = handPixels.clone();
- // Uzyskanie macierzy pikseli na podstawie obiektów Mat
- Mat_<int> handEdgesVerticalPixels = handEdgesVertical;
- Mat_<int> handEdgesHorizontalPixels = handEdgesHorizontal;
- int sobel[3][3]= {
- {1,2,1},
- {0,0,0},
- {-1,-2,-1}
- };
- int prewitt[3][3]={
- {-1,0,1},
- {-1,0,1},
- {-1,0,1}
- };
- for(int y = 1; y < handEdgesHorizontalPixels.rows - 1; y++){
- for(int x = 1; x < handEdgesHorizontalPixels.cols - 1; x++){
- int suma =
- handPixels[y-1][x-1]* sobel[0][0] +
- handPixels[y-1][x]* sobel[0][1] +
- handPixels[y-1][x+1]* sobel[0][2] +
- handPixels[y][x-1]* sobel[1][0] +
- handPixels[y][x]* sobel[1][1] +
- handPixels[y][x+1]* sobel[1][2] +
- handPixels[y+1][x-1]* sobel[2][0] +
- handPixels[y+1][x]* sobel[2][1] +
- handPixels[y+1][x+1]* sobel[2][2];
- handEdgesHorizontalPixels[y][x] = abs(suma);
- }
- }
- int max = handEdgesHorizontalPixels[0][0];
- int min = handEdgesHorizontalPixels[0][0];
- for(int i=0; i < handEdgesHorizontalPixels.rows; i++){
- for (int j=0; j < handEdgesHorizontalPixels.cols; j++){
- if (handEdgesHorizontalPixels[i][j] < min){
- min = handEdgesHorizontalPixels[i][j];
- }
- if (handEdgesHorizontalPixels[i][j] > max){
- max = handEdgesHorizontalPixels[i][j];
- }
- }
- }
- for(int y=1; y < handEdgesHorizontalPixels.rows - 1; y++){
- for (int x=1; x < handEdgesHorizontalPixels.cols - 1; x++){
- handEdgesHorizontalPixels[y][x] = ((handEdgesHorizontalPixels[y][x]-min)*(max-min))/(max-min);
- }
- }
- //handEdgesMergedPixels=handEdgesHorizontalPixels;
- for(int y = 1; y < handEdgesVerticalPixels.rows - 1; y++){
- for(int x = 1; x < handEdgesVerticalPixels.cols - 1; x++){
- int suma =
- handPixels[y-1][x-1]* prewitt[0][0] +
- handPixels[y-1][x]* prewitt[0][1] +
- handPixels[y-1][x+1]* prewitt[0][2] +
- handPixels[y][x-1]* prewitt[1][0] +
- handPixels[y][x]* prewitt[1][1] +
- handPixels[y][x+1]* prewitt[1][2] +
- handPixels[y+1][x-1]* prewitt[2][0] +
- handPixels[y+1][x]* prewitt[2][1] +
- handPixels[y+1][x+1]* prewitt[2][2];
- handEdgesVerticalPixels[y][x] = abs(suma);
- }
- }
- for(int i=0; i < handEdgesVerticalPixels.rows; i++) {
- for (int j=0; j < handEdgesVerticalPixels.cols; j++){
- if (handEdgesVerticalPixels[i][j] < min) {
- min = handEdgesVerticalPixels[i][j];
- }
- if (handEdgesVerticalPixels[i][j] > max){
- max = handEdgesVerticalPixels[i][j];
- }
- }
- }
- for(int y=0; y < handEdgesVerticalPixels.rows - 1; y++) {
- for (int x=0; x < handEdgesVerticalPixels.cols - 1; x++) {
- handEdgesVerticalPixels[y][x] = ((handEdgesVerticalPixels[y][x]-min)*(max-min))/(max-min);
- }
- }
- int minskala = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0];
- int maxskala = handEdgesVerticalPixels[0][0]+handEdgesHorizontalPixels[0][0];
- for(int i=0; i < handEdgesVerticalPixels.rows; i++){
- for(int j = 0; j < handEdgesVerticalPixels.cols; j++) {
- if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] > maxskala)
- maxskala = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
- if(handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j] < minskala)
- minskala = handEdgesVerticalPixels[i][j] + handEdgesHorizontalPixels[i][j];
- }
- }
- Mat_<int> got = handEdgesVertical;
- for(int y=0;y< handEdgesVerticalPixels.rows; y++){
- for(int x = 0; x < handEdgesVerticalPixels.cols; x++){
- got[y][x]=((handEdgesHorizontalPixels[y][x]+handEdgesVerticalPixels[y][x]-minskala)*(maxskala-minskala))/(maxskala-minskala);
- }
- }
- for(int y=0;y< got.rows; y++){
- for(int x = 0; x < got.cols; x++){
- if(got[y][x]>prog)
- got[y][x]=255;
- else got[y][x]=0;
- }
- }
- handEdgesMergedPixels = got;
- // TODO
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement