Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include "myqueue.cpp"
  2.  
  3. double getDistance2(CvPoint p1, CvPoint p2) {
  4. return (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
  5. }
  6.  
  7. void findConnectedRegion(IplImage *src, IplImage *dst, CvPoint begin, int &area, double &distance2) {
  8. area = 0;
  9. distance2 = -1;
  10. CvPoint tmp;
  11. CvPoint center = cvPoint(src->width / 2, src->height / 2);
  12. MyQueue *q = new MyQueue();
  13. q->push(begin);
  14. while (!q->empty()) {
  15. tmp = q->pop();
  16. if (tmp.x >= 0 && tmp.y >= 0 && tmp.x < src->width && tmp.y < src->height && cvGetReal2D(src, tmp.x, tmp.y) != 0) {
  17. cvSetReal2D(src, tmp.x, tmp.y, 0);
  18. cvSetReal2D(dst, tmp.x, tmp.y, 255);
  19. area++;
  20. if (getDistance2(tmp, center) < distance2 || distance2 < 0) {
  21. distance2 = getDistance2(tmp, center);
  22. }
  23. q->push(cvPoint(tmp.x - 1, tmp.y));
  24. q->push(cvPoint(tmp.x, tmp.y - 1));
  25. q->push(cvPoint(tmp.x + 1, tmp.y));
  26. q->push(cvPoint(tmp.x, tmp.y + 1));
  27. }
  28. }
  29. delete q;
  30. }
  31.  
  32. void main() {
  33. IplImage *frame = cvLoadImage("c:/Users/Username/Desktop/img.png", 0);
  34. IplImage *image = cvCloneImage(frame);
  35. int area;
  36. double distance2;
  37. cvNamedWindow("window");
  38. for (int i = 0; i < frame->height; i++) {
  39. for (int j = 0; j < frame->width; j++) {
  40. cvSetZero(image);
  41. findConnectedRegion(frame, image, cvPoint(i, j), area, distance2);
  42. if (area != 0) {
  43. cvShowImage("window", image);
  44. cvWaitKey();
  45. }
  46. }
  47. }
  48. cvDestroyAllWindows();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement