Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 188 lines (163 sloc) 6.165 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#include "cv.h"
#include "highgui.h"
#include "stdio.h"
#include "o2c3conv.h"
#include "imgutil.h"

int g_slider_position = 0;
CvCapture* capture = NULL;

int find_way(IplImage* in_img, IplImage* out_img, int pxdelta){
    //Najde cestu metodou porovnavani se stredovym pixelem
    //hleda v celem obrazku/roi
    imgdim in_img_dim;
    in_img_dim = get_dimensions(in_img);

    int refy, last_refy, worky1, worky2, workx, tmppxdiff;
    long total = 0;
    int tcount = 0;
    int middle = (in_img_dim.ystop - in_img_dim.ystart)/2;
    uchar *refpixel, *workpixel;

    refy = middle;

    last_refy = refy;

    //projizdi pracovni oblast zprava doleva
    for (workx=in_img_dim.xstop; workx >= in_img_dim.xstart; workx--) {

        //hleda rozdilny pixel smerem nahoru (k nule)
        worky1 = refy;
        worky2 = refy;
        refpixel = px_pos(in_img,workx, refy);
        //nahoru
        while (worky1 > in_img_dim.ystart){
            tmppxdiff = pxdiff(refpixel, px_pos(in_img, workx, worky1));
            if (tmppxdiff > pxdelta) {
                break;
            }
            worky1--;
        }
        //dolu
        while (worky2 < (in_img_dim.ystop-1)){
            if (pxdiff(refpixel, px_pos(in_img, workx, worky2)) > pxdelta) {
                break;
            }
            worky2++;
        }
        last_refy = refy;
        total = total + refy;
        tcount++;
        refy = (worky1 + worky2) / 2;

        workpixel = px_pos(out_img, workx, (refy+last_refy)/2);
        workpixel[0] = 0;
        workpixel[1] = 255;
        workpixel[2] = 128;

        workpixel = px_pos(out_img, workx, worky1);
        workpixel[0] = 128;
        workpixel[1] = 0;
        workpixel[2] = 128;

        workpixel = px_pos(out_img, workx, worky2);
        workpixel[0] = 128;
        workpixel[1] = 0;
        workpixel[2] = 128;

    }
    int direction = (total/tcount) - (in_img_dim.ystop - in_img_dim.ystart)/2;
    cvLine(out_img, cvPoint(0, middle + direction), cvPoint(tcount, middle+direction),
            cvScalar(0, 0, 255, 0), 1, 8, 0);
    return direction;
}


void onTrackbarSlide(int pos){
    cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, pos);
}


int main(int argc, char** argv){

    int lookup_table[256][256];
    int x,y;

    //prostor v kterem se dela detekce
    // pro avicka z robotur
    // cvRect(70,140, 130, 200)
    int roi_x = 80;
    int roi_y = 170;
    int roi_width = 110;
    int roi_height = 200;
    int fw_pxdiff = 20;
    int kam, kam2;

    // priprava lookup array pro c1c2c3
    for (x=0; x<= 255; x++){
        for(y=0; y<=255; y++) {
            lookup_table[x][y] = (int) (cvFastArctan(x,y) * 255 / 90);
        }
    }

    cvNamedWindow( "Display", CV_WINDOW_AUTOSIZE);
    cvNamedWindow( "o1o2", CV_WINDOW_AUTOSIZE);
    cvNamedWindow( "c1c2c3", CV_WINDOW_AUTOSIZE);
    printf("Argc %d", argc);
    if( argc==1 ) {
        capture = cvCreateCameraCapture(0);
    } else {
        capture = cvCreateFileCapture( argv[1] );
    }
    assert( capture != NULL );

    //int frames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
    //if (frames != 0){
    // cvCreateTrackbar("Position", "Display", &g_slider_position, frames, onTrackbarSlide);
    //}
    IplImage* frame;

    while(1){
        frame = cvQueryFrame(capture);
        if (!frame)
            break;
        // ROI omezuje konvertovanou oblast na tu kterou opravdu zkoumame
        cvSetImageROI(frame, cvRect(roi_x,roi_y, roi_width, roi_height));

        IplImage* newframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
        IplImage* newframe2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
        to_o1o2(frame,newframe);
        to_c1c2c3(frame, newframe2, lookup_table);

        cvRectangle(frame, cvPoint(0,0), cvPoint(roi_width-1, roi_height-1),
                cvScalar(0, 255, 0, 0), 1, 8, 0);
        kam = find_way(newframe, newframe, fw_pxdiff);
        kam2 = find_way(newframe2, newframe2, fw_pxdiff);
        cvLine(frame, cvPoint(0,roi_height/2), cvPoint(roi_width,roi_height/2),
                cvScalar(0, 255, 255, 0), 1, 8, 0);
        cvLine(frame, cvPoint(0,(roi_height+kam+kam2)/2), cvPoint(roi_width,(roi_height+kam+kam2)/2),
                cvScalar(0, 0, 255, 0), 1, 8, 0);
        cvResetImageROI(frame);
        printf("kam %d kam2 %d \n", kam, kam2);
        cvShowImage("Display", frame);
        cvShowImage("o1o2", newframe);
        cvShowImage("c1c2c3", newframe2);
        char c = cvWaitKey(1);
        if (c == 27) break;
        if (c > 0) {
            switch(c){
                //hjkl pro pohyb roi, se shiftem meni velikost
                case 104:
                             roi_x--; printf( "roi_x: %d", roi_x); break;
                case 108:
                             roi_x++; printf( "roi_x: %d", roi_x); break;
                case 106:
                             roi_y++; printf( "roi_y: %d", roi_y); break;
                case 107:
                             roi_y--; printf( "roi_y: %d", roi_y); break;
                case 72:
                            roi_width--; printf( "roi_width: %d", roi_width); break;
                case 76:
                            roi_width++; printf( "roi_width: %d", roi_width); break;
                case 75:
                            roi_height--; printf( "roi_height: %d", roi_height); break;
                case 74:
                            roi_height++;
                            printf( "roi_height: %d", roi_height);
                            break;
                case 32:
                            cvWaitKey(0); break;
                case 111:
                            fw_pxdiff--;
                            printf( "fw_pxdiff: %d", fw_pxdiff);
                            break;
                case 112:
                            fw_pxdiff++;
                            printf( "fw_pxdiff: %d", fw_pxdiff);
                            break;
            }

            printf("key %d \n", c);
        }
    }
    cvReleaseCapture( &capture);
    cvDestroyWindow( "Display");
    cvDestroyWindow( "o1o2");
    cvDestroyWindow( "c1c2c3");
}
Something went wrong with that request. Please try again.