Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "myqueue.cpp"
- double getDistance2(CvPoint p1, CvPoint p2) {
- return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
- }
- void findConnectedRegion(IplImage *src, IplImage *dst, CvPoint begin, int &area, double &distance2) {
- area = 0;
- distance2 = -1;
- CvPoint tmp;
- CvPoint center = cvPoint(src->width / 2, src->height / 2);
- MyQueue *q = new MyQueue();
- q->push(begin);
- while (!q->empty()) {
- tmp = q->pop();
- if (tmp.x >= 0 && tmp.y >= 0 && tmp.x < src->width && tmp.y < src->height && cvGetReal2D(src, tmp.x, tmp.y) != 0) {
- cvSetReal2D(src, tmp.x, tmp.y, 0);
- cvSetReal2D(dst, tmp.x, tmp.y, 255);
- area++;
- if (getDistance2(tmp, center) < distance2 || distance2 < 0) {
- distance2 = getDistance2(tmp, center);
- }
- q->push(cvPoint(tmp.x - 1, tmp.y));
- q->push(cvPoint(tmp.x, tmp.y - 1));
- q->push(cvPoint(tmp.x + 1, tmp.y));
- q->push(cvPoint(tmp.x, tmp.y + 1));
- }
- }
- delete q;
- }
- void main() {
- IplImage *frame = cvLoadImage("c:/Users/Username/Desktop/img.png", 0);
- IplImage *image = cvCloneImage(frame);
- int area;
- double distance2;
- cvNamedWindow("window");
- for (int i = 0; i < frame->height; i++) {
- for (int j = 0; j < frame->width; j++) {
- cvSetZero(image);
- findConnectedRegion(frame, image, cvPoint(i, j), area, distance2);
- if (area != 0) {
- cvShowImage("window", image);
- cvWaitKey();
- }
- }
- }
- cvDestroyAllWindows();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement