在OpenCV中检测拳击对象

时间:2015-10-15 11:06:30

标签: opencv visual-c++

我目前正在实现连接组件算法,算法的最后一步要求我将包含在框中的对象包含在内。我试图在框中包含一个对象,结果就是这样:

Boxed Image Result

正如您所看到的,其中一些似乎被封装在一个盒子里。除非我拉伸imshow函数输出的窗口,并且当我预期有一条灰色阴影的线条时,它们中的一些还有颜色,所以看不到盒子的某些线条。

我的问题是:对象是否真的被封闭,因为我记得当我将类似的代码运行到不同的操作系统时,有颜色的线条根本看不到,但在我的电脑中看到。另外,为什么有些线条呈现出不同的颜色,因为我期待一片灰色。

Mat src, src_gray;
Mat dst, detected_edges;

const char* window_name = "THRESHOLDED IMAGE";

/**
 * @function connectedComponent
 */
static void connectedComponent(int, void*)
{


    Mat test;       //dummy
    Mat sub;
    int newObject = 0;
    int zeroTest = 0, nonZero = 0;
    int arr[5] = {0,0,0,0,0};
    /// Reduce noise with a kernel 3x3
    blur( src_gray,detected_edges, Size(3,3) );         //filtering out of noise

    namedWindow("INITIAL", WINDOW_NORMAL);
    imshow("INITIAL",detected_edges);
    resizeWindow("INITIAL", 300, 300);
    threshold(detected_edges, detected_edges, 0,255, THRESH_BINARY | THRESH_OTSU);

    int** newSub = new int*[detected_edges.rows];
        for(int i = 0; i < detected_edges.rows; i++)
            newSub[i] = new int[detected_edges.cols];

    for(int i = 0; i < detected_edges.rows; i++){
        for(int j = 0; j < detected_edges.cols; j++){
            newSub[i][j] = 0;
        }
    }
/*INITIAL MARKING LOOP*/

    for(int i = 0; i < detected_edges.rows; i++){
        for(int j = 0; j < detected_edges.cols; j++){
            if(detected_edges.at<uchar>(i,j) == 0){
                if(i-1 < 0 && j-1 < 0){
                    newObject = newObject + 1;      //no values
                    newSub[i][j] = newObject;
                }else if(i-1 >= 0 && j-1 < 0){
                    if(newSub[i-1][j] != 0){
                        newSub[i][j] = newSub[i-1][j];              //only up has value
                    }else{
                        newObject = newObject + 1;                                              //no values
                        newSub[i][j] = newObject;
                    }
                }else if(i-1 < 0 && j-1 >= 0){
                    if(newSub[i-1][j] != 0){
                        newSub[i][j] = newSub[i-1][j];              //only left has value
                    }else{
                        newObject = newObject + 1;                                  //no values
                        newSub[i][j] = newObject;
                    }
                }else{
                    if(newSub[i-1][j] == 0 && newSub[i][j-1] == 0){
                        newObject = newObject + 1;                                              //no values
                        newSub[i][j] = newObject;
                    }else if(newSub[i-1][j] == newSub[i][j-1]){             //same value
                        newSub[i][j] = newSub[i-1][j];
                    }else if((newSub[i-1][j] != 0 && newSub[i][j-1] == 0)){
                        newSub[i][j] = newSub[i-1][j];              //only up has value
                    }else if(newSub[i-1][j] == 0 && newSub[i][j-1] != 0 ){
                        newSub[i][j] = newSub[i][j-1];              //only left has value
                    }else if(newSub[i-1][j] != newSub[i][j-1]){
                        newSub[i][j] = newSub[i-1][j];                  //different values follow upper's value
                    }
                }
            }
        }
    }

    int a = 1;
    int maxRows = detected_edges.rows;
    int maxCols = detected_edges.cols;
/*CONNECTING PIXELS RIGHT-BOTTOM*/
    while(a < newObject){
        int update = 0;
        for(int i = 0; i < maxRows; i++){
            for(int j = 0; j < maxCols; j++){
                if(newSub[i][j] == a){
                    if(i+1 < maxRows && j+1 < maxCols){
                        if(newSub[i][j+1] > a){             //both points allowed
                            int value = newSub[i][j+1];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){          //replace all instances of that value
                                        newSub[h][k] = a;

                                    }
                                }
                            }

                            update = 1;
                        }
                        if(newSub[i+1][j] > a){             //both points allowed
                            int value = newSub[i+1][j];     //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){
                                        newSub[h][k] = a;           //replace all instances of that value
                                    }
                                }
                            }

                            update = 1;
                        }

                    }else if(i+1 > maxRows && j+1 < maxCols){
                        if(newSub[i][j+1] > a){                 //bottom is not allowed
                            int value = newSub[i][j+1];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){
                                        newSub[h][k] = a;       //replace all instances of that value

                                    }
                                }
                            }
                            update = 1;

                        }                                       
                    }else if(i+1 < maxRows && j+1 > maxCols){
                        if(newSub[i+1][j] > a){                 //right is not allowed
                            int value = newSub[i+1][j];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){      //replace all instances of that value
                                        newSub[h][k] = a;
                                    }
                                }
                            }
                            update = 1;
                        }                                       
                    }
                }
            }
        }
        a++;
    }
    /*CONNECTING PIXELS LEFT-TOP*/
    a = newObject;
    while(a > 0){
        int update = 0;
        for(int i = maxRows-1; i > 0; i--){
            for(int j = maxCols-1; j > 0 ; j--){
                if(newSub[i][j] == a){
                    if(i-1 >= 0 && j-1 >= 0){
                        if(newSub[i][j-1] > a){             //both points allowed
                            int value = newSub[i][j-1];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){          
                                        newSub[h][k] = a;
                                    }
                                }
                            }
                            update = 1;
                        }
                        if(newSub[i-1][j] > a){             
                            int value = newSub[i-1][j];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){
                                    if(newSub[h][k] == value){      //replace all instances of that value
                                        newSub[h][k] = a;
                                    }
                                }
                            }   
                            update = 1;
                        }

                    }else if(i-1 >= 0 && j-1 < 0){
                        if(newSub[i][j-1] > a){                 //left is not allowed
                            int value = newSub[i][j-1];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){
                                for(int k = 0; k < maxCols; k++){   //replace all instances of that value
                                    if(newSub[h][k] == value){
                                        newSub[h][k] = a;

                                    }
                                }
                            }
                            update = 1;         
                        }                                       
                    }else if(i-1 < 0 && j-1 >= 0){
                        if(newSub[i-1][j] > a){                 //top is not allowed
                            int value = newSub[i-1][j];         //get the value I need to replace
                            for(int h = 0; h < maxRows; h++){   
                                for(int k = 0; k < maxCols; k++){   //replace all instances of that value
                                    if(newSub[h][k] == value){
                                        newSub[h][k] = a;
                                    }
                                }
                            }
                            update = 1;
                        }                                       
                    }
                }
            }
        }
        a--;
    }

    for(int i = 0; i < maxRows; i++){
        for(int j = 0; j < maxCols; j++){
            int check = 0;
            if(newSub[i][j] != 0){
                for(int k = 0; k < 5; k++){
                    if(newSub[i][j] == arr[k]){         //check if there is an instance of the value in the given array of values
                        check = 1;
                        break;
                    }
                }

                if(check == 0){
                    for(int r = 0; r < 5; r++){
                        if(arr[r] == 0){
                            arr[r] = newSub[i][j];      //if new value is found add to array
                            break;
                        }
                    }
                }
            }       
        }
    }

/*
I HAVE AN ARRAY CONTAINING ALL VALUES
**/

    src.copyTo( sub, detected_edges);
    sub = Scalar::all(0);

/*SET AN INTENSITY FOR CORRESPONDING VALUES*/
    int intensity = 50;
    a = 0;
    while(a < 5){
        int update = 0;
        for(int i = 0; i < maxRows; i++){
            for(int j = 0; j < maxCols; j++){
                    if(newSub[i][j] == arr[a]){
                        sub.at<uchar>(i,j) = intensity;
                    }
            }
        }
        a++;
        intensity = intensity + 50;
    }

    a = 250; 
    /*GETTING MIN-MAX COORDINATES*/
    while(a >= 50){
        int setter = 0;
        int minRow = 0;
        int minCol = 0;
        int maxRow = 0;
        int maxCol = 0;
        for(int i = 0; i < maxRows; i++){
            for(int j = 0; j < maxCols; j++){
                if(sub.at<uchar>(i,j) == a){
                    if(setter == 0){
                        minRow = i;
                        minCol = j;
                        maxRow = i;
                        maxCol = j;
                        setter = 1;
                    }else{
                        if(i <= minRow){
                            minRow = i;
                        }
                        else{
                            if(i > maxRow){
                                maxRow = i;
                            }
                        }
                        if(j <= minCol){
                            minCol = j;
                        }
                        else{
                            if(j > maxCol){
                                maxCol = j;
                            }
                        }
                    }
                }   
            }
        }
        /*THIS IS WHERE I MAKE MY BOUNDING BOX*/
        for(int i = minRow; i < maxRow; i++){
            sub.at<uchar>(i,minCol) = 255;          //set up the horizontal lines
            sub.at<uchar>(i,maxCol) = 255;
        }
        for(int i = minCol; i < maxCol; i++){
            sub.at<uchar>(minRow,i) = 255;          //set up the vertical lines
            sub.at<uchar>(maxRow,i) = 255;
        }

        a = a - 50;
    }


    dst = Scalar::all(0);
    src.copyTo( dst, detected_edges);
    imshow( window_name, dst );

    namedWindow("FINAL", WINDOW_NORMAL);
    imshow("FINAL",sub);                    //final output
    resizeWindow("FINAL", 300, 300);

    for(int i = 0; i < detected_edges.rows; i++)
        delete[] newSub[i];
    delete[] newSub;
}


/**
 * @function main
 */
int main( int, char** argv )
{
  /// Load an image
  src = imread( argv[1] );

  if( src.empty() )
    { return -1; }

  /// Create a matrix of the same type and size as src (for dst)
  dst.create( src.size(), src.type() );

  /// Convert the image to grayscale
  cvtColor( src, src_gray, COLOR_BGR2GRAY );        //grayscale for one channel for easy computation

  /// Create a window
  namedWindow( window_name, WINDOW_NORMAL );
  resizeWindow(window_name, 300,300);

  /// Show the image
  connectedComponent(0, 0);

  /// Wait until user exit program by pressing a key
  waitKey(0);

  return 0;
}

0 个答案:

没有答案