{"id":89,"date":"2010-12-04T08:01:59","date_gmt":"2010-12-04T00:01:59","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=89"},"modified":"2012-03-03T08:32:20","modified_gmt":"2012-03-03T00:32:20","slug":"lab-7%ef%bc%8copencv","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/lab-7%ef%bc%8copencv\/","title":{"rendered":"Lab-7\uff0cOpenCV &#8230;"},"content":{"rendered":"<p>Nov 29th\uff0c\u53bb\u4e86\u4e00\u4e0b\u697c\u4e0a\u5bdd\u5ba4\u819c\u62dc\u4e86\u4e00\u4e0b\u6c99\u6f20\u541b\uff0c\u5f97\u5230\u4e86\u5b83\u4eec\u76ee\u524d\u7684\u5de5\u4f5c\u8fdb\u5c55\uff0c\u662f\u4e00\u4efd\u9ad8\u7ea7\u7a0b\u5e8f\u8bed\u8a00\u7684\u4f5c\u4e1a &#8230; \u554a\uff0c\u5f53\u65f6\u6211\u5c31\u60ca\u4e86&#8230;<br \/>\n<!--more--><br \/>\n<a href=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/2010\/12\/save.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.shuizilong.com\/house\/wp-content\/uploads\/2010\/12\/save-300x187.jpg\" alt=\"\" title=\"save\" width=\"300\" height=\"187\" class=\"aligncenter size-medium wp-image-2017\" \/><\/a><\/p>\n<p>xiaodai : &#8220;\u6211\u7684\u8fd9\u4e2a\u8fd8\u4e0d\u662f\u5f88\u597d\u7684\uff0cXueyr \u7684\u4f5c\u4e1a\u5728\u7f29\u653e\u7684\u65f6\u4faf\u7528\u4e86\u591a\u7ebf\u7a0b\u3002\u3002\u3002&#8221;<br \/>\nxiaodao \uff1a&#8221;\u3002\uff03\u554a\u554a\u554a\u554a\u3002\u3002\u3002\u8981\u600e\u4e48\u5728 Xcode \u91cc\u5b9e\u73b0\u591a\u5e27\u5237\u5c4f\u5462\uff1f\u3002\u3002&#8221;<\/p>\n<pre lang=\"cpp\" file=\"xiaodai.cpp\">\r\n\/\/by SHA Mo\r\n\/\/Visual Studio 2010 + OpenCV 2.1\r\n\r\n\r\n#include <cv.h>\r\n#include <highgui.h>\r\n#include <cmath>\r\n#include <cstring>\r\n#include <cvaux.h>\r\n#include <iostream>\r\n\r\nusing namespace std;\r\n\r\n\/\/\u58f0\u660e\u9f20\u6807\u56de\u8c03\u51fd\u6570\r\nvoid my_mouse_callback(int event, int x, int y, int flags, void* param);\r\n\r\n\/\/\u56fe\u7684\u5206\u8fa8\u7387\r\nconst int IMAGE_SIZE_WID = 640;\r\nconst int IMAGE_SIZE_HGT = 480;\r\n\r\n\/\/\u81ea\u52a8\u7f29\u653e\u56fe\u50cf\u5206\u5272\u6570\r\nconst int split_wid = 4;\r\nconst int split_hgt = 4;\r\n\r\n\/\/\u8fed\u4ee3\u6b21\u6570\u4e0a\u754c\r\nconst int ITERATION_TIME = 200;\r\n\r\n\/\/\u8fc7\u5ea6\u52a8\u753b\u5e27\u6570\r\nconst int TRANSITION_IMAGE = 20;\r\n\r\n\/\/\u8fc7\u5ea6\u52a8\u753b\u5e27\u901f\r\nconst int TRANSITION_TIME = 1;\r\n\r\n\/\/\u8331\u8389\u4e9a\u96c6\u5408\u53c2\u6570\r\ndouble j_re = -0.835;\r\ndouble j_im = -0.2350;\r\n\r\n\/\/\u56fe\u7684\u7f29\u653e\u6bd4\r\ndouble zoomRatio_WID = 4;\r\ndouble zoomRatio_HGT = 3;\r\n\r\n\/\/\u51fd\u6570\u56fe\u8c61\u7684\u5e73\u79fb\u91cf\r\ndouble mov_re = -2.5;\r\ndouble mov_im = -1.5;\r\n\r\n\/\/\u4e0a\u8272\u63a7\u91cf\r\nconst double PI = acos(-1.0);\r\ndouble ratio_r = 30;\t\t\r\ndouble ratio_g = 25;\r\ndouble ratio_b = 20;\r\ndouble phase_r = 0;\t\t\t\r\ndouble phase_g = 0;\r\ndouble phase_b = 0.25;\r\n\r\n\/\/\u9f20\u6807\u4e8b\u4ef6\u6240\u9700\u91cf\r\nCvRect box;\r\nbool drawing_box = false;\r\nbool moving = false;\r\nint mov_x;\r\nint mov_y;\r\n\r\n\/\/\u9f20\u6807\u5b9e\u65f6\u4f4d\u7f6e\r\nCvPoint mousePoint;\r\n\r\n\/\/\u5224\u65ad\u4e00\u4e2a\u590d\u6570\u662f\u5426\u5c5e\u4e8eMandelbrot_Set\r\nint mSetTest(double c_re, double c_im) {\r\n\tint current_time = 1;\r\n\tdouble z_re = 0;\r\n\tdouble z_im = 0;\r\n\twhile ( z_re*z_re + z_im*z_im < 4 &#038;&#038; current_time < ITERATION_TIME) {\r\n\t\t++current_time;\r\n\t\tdouble t_re = z_re*z_re - z_im*z_im + c_re;\r\n\t\tz_im = 2*z_re*z_im + c_im;\r\n\t\tz_re = t_re;\r\n\t}\r\n\tif (current_time == ITERATION_TIME) return 0;\r\n\telse return current_time;\r\n}\r\n\r\n\/\/\u5224\u65ad\u4e00\u4e2a\u590d\u6570\u662f\u5426\u5c5e\u4e8eJulia_Set\r\nint jSetTest(double c_re, double c_im) {\r\n\tint current_time = 1;\r\n\twhile ( c_re*c_re + c_im*c_im < 4 &#038;&#038; current_time < ITERATION_TIME) {\r\n\t\t++current_time;\r\n\t\tdouble t_re = c_re*c_re - c_im*c_im + j_re;\r\n\t\tc_im = 2*c_re*c_im + j_im;\r\n\t\tc_re = t_re;\r\n\t}\r\n\tif (current_time == ITERATION_TIME) return 0;\r\n\telse return current_time;\r\n}\r\n\r\n\/\/\u8ba1\u7b97\u50cf\u7d20\u70b9\u7684\u989c\u8272\r\nCvScalar whatColor(int x, int y) {\r\n\t\/\/\u4e03\u5f69\u989c\u8272\u5e93\uff1a\u7ea2\u6a59\u9ec4\u7eff\u84dd\u975b\u7d2b\r\n\tCvScalar colorLib[7] = {{0, 0, 255},{15, 123, 255},{15, 255, 255},{15, 255, 33},{255, 141, 16},{255, 15, 69},{201, 15, 255}};\r\n\tCvScalar black = {0, 0, 0};\r\n\tCvScalar color;\r\n\tint step = jSetTest(mov_re + x\/(double)IMAGE_SIZE_WID*zoomRatio_WID, mov_im + y\/(double)IMAGE_SIZE_HGT*zoomRatio_HGT);\r\n\tif (step) {\r\n\t\t\/\/\u4e0d\u5c5e\u4e8e\u5206\u578b\u96c6\u5408\r\n\t\t\/\/color = colorLib[(step + 5) % 7];\r\n\t\tcolor.val[0] = 127.5 - 127.5 * cos(step \/ ratio_b + phase_b * PI);\r\n\t\tcolor.val[1] = 127.5 - 127.5 * cos(step \/ ratio_g + phase_g * PI);\r\n\t\tcolor.val[2] = 127.5 - 127.5 * cos(step \/ ratio_r + phase_r * PI);\r\n\t}\r\n\telse {\r\n\t\t\/\/\u5c5e\u4e8e\u5206\u578b\u96c6\u5408\r\n\t\tcolor = black;\r\n\t}\r\n\treturn color;\r\n}\r\n\r\n\/\/\u7ed8\u5236\u9009\u5b9a\u6846\r\nvoid draw_box( IplImage* img, CvRect rect ) {\r\n\tcvRectangle (\r\n\t\timg,\r\n\t\tcvPoint(box.x, box.y),\r\n\t\tcvPoint(box.x+box.width, box.y+box.height),\r\n\t\tcvScalar(0x00, 0xff, 0x00)\r\n\t\t);\r\n}\r\n\r\n\/\/\u7ed8\u5236\u56fe\u50cf\r\nvoid draw( IplImage* image ) {\r\n\tfor (int i = 0; i < IMAGE_SIZE_WID; ++i)\r\n\t\tfor (int j = 0; j < IMAGE_SIZE_HGT; ++j) {\r\n\t\t\tcvSet2D(image, j, i, whatColor(i, j));\r\n\t\t}\r\n}\r\n\r\n\/\/\u81ea\u5b9a\u4e49\u53ef\u4ee5\u8d8a\u754c\u7684\u53d6\u8272\u51fd\u6570\r\nCvScalar my_cvGet2D(IplImage* image, int x, int y)\r\n{\r\n\t\/\/\u8d8a\u754c\u8fd4\u56de\u9ed1\u8272\r\n\tif (x<0 || y<0 || x>=IMAGE_SIZE_WID || y>=IMAGE_SIZE_HGT) return cvScalar(0x00, 0x00, 0x00);\r\n\treturn cvGet2D(image, y, x);\r\n}\r\n\r\n\/\/\u62f7\u8d1d\u56fe\u50cf\u77e9\u5f62\u533a\u57df\uff08\u5141\u8bb8\u8d8a\u754c\uff0c\u8d8a\u754c\u90e8\u5206\u586b\u9ed1\uff0cROI\u5b9e\u73b0\uff09\r\nIplImage* my_cvCloneImageRect(IplImage* src, CvRect rect) {\r\n\tIplImage *dst = cvCreateImage(cvSize(rect.width, rect.height), src->depth, src->nChannels);\r\n\tcvSetZero(dst);\r\n\r\n\t\/\/\u8ba1\u7b97src\u7684ROI\u77e9\u5f62\r\n\tCvRect src_rect;\r\n\tsrc_rect.x = max(0, rect.x);\r\n\tsrc_rect.y = max(0, rect.y);\r\n\tsrc_rect.width = min(src->width, rect.x + rect.width) - src_rect.x;\r\n\tsrc_rect.height = min(src->height, rect.y + rect.height) - src_rect.y;\r\n\tif (src_rect.width <= 0 || src_rect.height <= 0) {\r\n\t\tsrc_rect.width = 0;\r\n\t\tsrc_rect.height = 0;\r\n\t}\r\n\r\n\t\/\/\u8ba1\u7b97dst\u7684ROI\u77e9\u5f62\r\n\tCvRect dst_rect;\r\n\tdst_rect.x = max(0, -rect.x);\r\n\tdst_rect.y = max(0, -rect.y);\r\n\tdst_rect.width = min(rect.width, -rect.x + src->width) - dst_rect.x;\r\n\tdst_rect.height = min(rect.height, -rect.y + src->height) - dst_rect.y;\t\r\n\r\n\tif (dst_rect.width <= 0 || dst_rect.height <= 0) {\r\n\t\tdst_rect.width = 0;\r\n\t\tdst_rect.height = 0;\r\n\t}\r\n\tif (src_rect.width!=0 &#038;&#038; src_rect.height!=0 &#038;&#038; dst_rect.width!=0 &#038;&#038; dst_rect.height!=0) {\r\n\t\tcvSetImageROI(src, src_rect);\r\n\t\tcvSetImageROI(dst, dst_rect);\r\n\t\tcvResize(src, dst);\r\n\t\tcvResetImageROI(src);\r\n\t\tcvResetImageROI(dst);\r\n\t}\r\n\treturn dst;\r\n}\r\n\r\n\/\/\u663e\u793a\u8fc7\u5ea6\u6548\u679c\r\nIplImage* show_transition(IplImage* image, CvRect &#038;box) {\r\n\t\r\n\tIplImage* tmp = cvCloneImage( image );\r\n\tfor (int k = 1; k <= TRANSITION_IMAGE; ++k) {\r\n\r\n\t\t\/\/\u8ba1\u7b97\u5206\u6b65\u7f29\u653e\u7684\u4e34\u65f6\u9009\u5b9a\u6846\r\n\t\tdouble tmp_x = box.x * (double)k\/TRANSITION_IMAGE;\r\n\t\tdouble tmp_y = box.y * (double)k\/TRANSITION_IMAGE;\r\n\t\tdouble tmp_width = IMAGE_SIZE_WID - (IMAGE_SIZE_WID - box.x - box.width) * (double)k\/TRANSITION_IMAGE - tmp_x;\r\n\t\tdouble tmp_height = IMAGE_SIZE_HGT - (IMAGE_SIZE_HGT - box.y - box.height) * (double)k\/TRANSITION_IMAGE - tmp_y;\r\n\r\n\t\tCvRect tmp_box = cvRect(tmp_x, tmp_y, tmp_width, tmp_height);\r\n\t\tcvResize(my_cvCloneImageRect(image, tmp_box), tmp);\r\n\t\tcvShowImage(\"HelloOpenCV\", tmp);\r\n\t\tcvWaitKey( TRANSITION_TIME );\r\n\t}\r\n\treturn tmp;\r\n}\r\n\r\n\/\/\u79fb\u52a8\u7f29\u653e\u56fe\u50cf\uff08\u589e\u52a0\u52a8\u6001\u52a8\u753b\u6548\u679c\uff09\r\nvoid move_zoom_image(IplImage* image, CvRect &#038;box, bool transition = 1) {\r\n\tIplImage* current_image = image;\r\n\r\n\t\/\/\u663e\u793a\u8fc7\u5ea6\u6548\u679c\r\n\tif (transition) {\r\n\t\tcurrent_image = show_transition( image, box );\r\n\t}\r\n\r\n\t\/\/\u6539\u53d8\u51fd\u6570\u56fe\u8c61\u7684\u5e73\u79fb\u91cf\r\n\tmov_re += (double)box.x\/IMAGE_SIZE_WID*zoomRatio_WID;\r\n\tmov_im += (double)box.y\/IMAGE_SIZE_HGT*zoomRatio_HGT;\r\n\r\n\t\/\/\u6539\u53d8\u56fe\u7684\u7f29\u653e\u6bd4\r\n\tzoomRatio_WID *= (double)box.width\/IMAGE_SIZE_WID;\r\n\tzoomRatio_HGT *= (double)box.height\/IMAGE_SIZE_HGT;\r\n\r\n\t\/\/\u5237\u65b0\u56fe\u50cf\r\n\tdraw( image );\r\n}\r\n\r\n\/\/\u7070\u5ea6\u56fe\uff0c\u5f00\u5728\u51fd\u6570\u5185\u90e8\u4f1a\u6808\u6ea2\u51fa\r\nint grey_level[IMAGE_SIZE_WID][IMAGE_SIZE_HGT];\r\n\/\/\u8bc4\u4f30\u56fe\u50cf\u590d\u6742\u5ea6\uff08\u591a\u7b97\u5b50\u52a0\u6743\uff1a\u4fe1\u606f\u71b5\u3001\u53cd\u5dee[\u7070\u5ea6\u77e9\u9635\u6c42\u6cd5]\u3001\u8fb9\u7f18\u6bd4\u7387\uff09\r\ndouble image_rect_complexity( IplImage* image, CvRect rect ) {\r\n\t\/\/\u5404\u7070\u5ea6\u50cf\u7d20\u6570\r\n\tint grey_level_count[256] = {};\r\n\r\n\t\/\/\u7070\u5ea6\u603b\u91cf\r\n\tint grey_level_num = rect.width * rect.height;\r\n\r\n\t\/\/\u7070\u5ea6\u5171\u751f\u77e9\u9635\r\n\tint GLCM[256][256] = {};\r\n\r\n\t\/\/\u62bd\u53d6\u56fe\u50cf\u7070\u5ea6\u4fe1\u606f\r\n\tfor (int i = rect.x; i < rect.x + rect.width; ++i)\r\n\t\tfor (int j = rect.y; j < rect.y + rect.height; ++j) {\r\n\t\t\tgrey_level[i][j]  = ((uchar*)(image->imageData + j*image->widthStep))[i*image->nChannels + 0];\r\n\t\t\tgrey_level[i][j] += ((uchar*)(image->imageData + j*image->widthStep))[i*image->nChannels + 1];\r\n\t\t\tgrey_level[i][j] += ((uchar*)(image->imageData + j*image->widthStep))[i*image->nChannels + 2];\r\n\t\t\tgrey_level[i][j] \/= 3;\r\n\t\t\t++grey_level_count[ grey_level[i][j] ];\r\n\t\t}\r\n\r\n\t\/\/\u8ba1\u7b97\u7070\u5ea6\u5171\u751f\u77e9\u9635\uff08\u25b3x=1\uff0c\u25b3y=1\uff09\u548c \u8fb9\u7f18\u50cf\u7d20\u6570\r\n\tint edge_pixel = 0;\r\n\tfor (int i = rect.x; i < rect.x + rect.width - 1; ++i)\r\n\t\tfor (int j = rect.y; j < rect.y + rect.height - 1; ++j) {\r\n\t\t\t++GLCM[grey_level[i][j]][grey_level[i+1][j+1]];\r\n\t\t\tif (grey_level[i][j] != grey_level[i+1][j+1]) ++edge_pixel;\r\n\t\t}\r\n\r\n\r\n\t\/\/\u8ba1\u7b97\u4fe1\u606f\u71b5\r\n\tdouble inf_entropy = 0;\r\n\tfor (int i = 0; i < 256; ++i) if (grey_level_count[i] != 0) {\r\n\t\tinf_entropy -= (double)grey_level_count[i]\/grey_level_num * log((double)grey_level_count[i]\/grey_level_num);\r\n\t}\r\n\r\n\t\/\/\u8ba1\u7b97\u53cd\u5dee\r\n\tdouble contrast = 0;\r\n\tfor (int i = 0; i < 256; ++i)\r\n\t\tfor (int j = 0; j < 256; ++j) if (GLCM[i][j] != 0)\r\n\t\t\tcontrast = (double)GLCM[i][j]\/grey_level_num * (i - j) * (i - j);\r\n\r\n\t\/\/\u8fb9\u7f18\u6bd4\u7387\r\n\tdouble edge_ratio = edge_pixel \/ grey_level_num;\r\n\r\n\t\/\/\u8fd4\u56de\u52a0\u6743\u56fe\u50cf\u590d\u6742\u5ea6\r\n\treturn contrast\/2 + inf_entropy + edge_ratio;\r\n}\r\n\r\n\/\/\u81ea\u52a8\u7f29\u653e\r\nvoid auto_zoom( IplImage* image ) {\r\n\tdouble max_complexity = 0;\r\n\tCvRect max_complex_rect;\r\n\tfor (int i = 0; i < split_wid; ++i)\r\n\t\tfor (int j = 0; j < split_hgt; ++j) {\r\n\t\t\tdouble tmp_complexity = image_rect_complexity( image, \r\n\t\t\t\tcvRect(\ti*(IMAGE_SIZE_WID\/split_wid), \r\n\t\t\t\t\t\tj*(IMAGE_SIZE_HGT\/split_hgt),\r\n\t\t\t\t\t\tIMAGE_SIZE_WID\/split_wid,\r\n\t\t\t\t\t\tIMAGE_SIZE_HGT\/split_hgt ) );\r\n\t\t\tif (tmp_complexity > max_complexity) {\r\n\t\t\t\tmax_complex_rect = cvRect(\t\r\n\t\t\t\t\ti*(IMAGE_SIZE_WID\/split_wid), \r\n\t\t\t\t\tj*(IMAGE_SIZE_HGT\/split_hgt),\r\n\t\t\t\t\tIMAGE_SIZE_WID\/split_wid,\r\n\t\t\t\t\tIMAGE_SIZE_HGT\/split_hgt );\r\n\t\t\t\tmax_complexity = tmp_complexity;\r\n\t\t\t}\r\n\t\t}\r\n\tmove_zoom_image( image, max_complex_rect );\r\n}\r\n\r\nint main( int argc, char* argv[] ) {\r\n\t\/\/\u521d\u59cb\u5316\u9009\u5b9a\u6846\u53d8\u91cf\r\n\tbox = cvRect(-1, -1, 0, 0);\r\n\r\n\t\/\/\u521b\u5efa\u56fe\u50cf\r\n\tIplImage* pImg = cvCreateImage (\/*size*\/cvSize (IMAGE_SIZE_WID, IMAGE_SIZE_HGT), \/*depth*\/IPL_DEPTH_8U, \/*nChannels*\/3);\r\n\tcvSetZero(pImg);\r\n\tIplImage* temp = cvCloneImage( pImg );\r\n\r\n\t\/\/\u521b\u5efa\u7a97\u53e3\r\n\tcvNamedWindow (\/*name of the window*\/\"HelloOpenCV\", 1);\r\n\r\n\t\/\/\u6807\u5fd7\u9f20\u6807\u4e8b\u4ef6\r\n\tcvSetMouseCallback(\"HelloOpenCV\", my_mouse_callback, (void*) pImg);\r\n\r\n\t\/\/\u521d\u59cb\u5316\u56fe\u50cf\r\n\tdraw( pImg );\r\n\r\n\t\/\/\u53cd\u590d\u83b7\u53d6\u7528\u6237\u4ea4\u4e92\u4fe1\u606f\u5e76\u5904\u7406\r\n\twhile ( 1 ) {\r\n\t\tCvFont font;\r\n\t\tdouble hScale = 1.0;\r\n\t\tdouble vScale = 1.0;\r\n\t\tint lineWidth = 1;\r\n\t\tcvCopyImage( pImg, temp );\r\n\r\n\t\t\/\/\u5b9e\u65f6\u7ed8\u5236\u9009\u5b9a\u6846\r\n\t\tif ( drawing_box ) draw_box( temp, box );\r\n\r\n\t\t\/\/\u6309\u4f4f\u53f3\u952e\u65f6\u62d6\u52a8\u753b\u9762\r\n\t\tif ( moving ) {\r\n\t\t\tCvRect tmp_box = cvRect(mov_x - mousePoint.x , mov_y - mousePoint.y, pImg->width, pImg->height);\r\n\t\t\tcvResize(my_cvCloneImageRect(pImg, tmp_box), temp);\r\n\t\t}\r\n\r\n\t\t\/\/\u5b9e\u65f6\u663e\u793a\u9f20\u6807\u4f4d\u7f6e\u5bf9\u5e94\u7684\u590d\u6570\u5750\u6807\u7cfb\u7684\u5750\u6807\r\n\t\tcvInitFont(&font, CV_FONT_HERSHEY_DUPLEX, hScale, vScale, 0, lineWidth);\r\n\r\n\t\tchar x_coord[10];\r\n\t\titoa((int)mousePoint.x, x_coord, 10);\r\n\t\tchar y_coord[10];\r\n\t\titoa((int)mousePoint.y, y_coord, 10);\r\n\t\tcvPutText(temp, strcat(strcat(x_coord, \",\"), y_coord), mousePoint, &font, cvScalar(0x00, 0x00, 0xff));\r\n\t\tcvShowImage( \"HelloOpenCV\", temp);\r\n\r\n\t\t\/\/\u8f93\u5165ESC\u540e\u9000\u51fa\r\n\t\tint key_num = cvWaitKey( 20 );\r\n\t\tif ( key_num == 27 ) break;\r\n\t\telse if ( key_num == 'r') {\r\n\t\t\tCvRect normal =  cvRect(0, 0, pImg->width, (int)(pImg->height \/ ((zoomRatio_HGT\/zoomRatio_WID)\/((double)IMAGE_SIZE_HGT\/IMAGE_SIZE_WID))));\r\n\t\t\tmove_zoom_image( pImg, normal );\r\n\t\t\t\/\/zoomRatio_HGT = zoomRatio_WID * IMAGE_SIZE_HGT \/ IMAGE_SIZE_WID;\r\n\t\t\t\/\/draw( pImg );\r\n\t\t}\r\n\t\telse if ( key_num == 'c') auto_zoom( pImg );\r\n\t}\r\n\t\r\n\t\/\/\u5173\u95ed\u7a97\u53e3\u3001\u91ca\u653e\u56fe\u50cf\r\n\tcvReleaseImage( &pImg );\r\n\tcvReleaseImage( &temp );\r\n\tcvDestroyWindow( \"HelloOpenCV\" );\r\n\treturn 0;\r\n}\r\n\r\n\/\/\u5b9a\u4e49\u9f20\u6807\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\r\nvoid my_mouse_callback(int event, int x, int y, int flags, void* param) {\r\n\t\r\n\tIplImage* image = (IplImage*) param;\r\n\tswitch ( event ) {\r\n\r\n\t\/\/\u9f20\u6807\u62d6\u52a8\r\n\tcase CV_EVENT_MOUSEMOVE :\r\n\r\n\t\tmousePoint = cvPoint(x, y);\r\n\t\t\/\/\u5de6\u952e\u62d6\u52a8\u65f6\u5b9e\u65f6\u7ef4\u62a4\u9009\u5b9a\u6846\r\n\t\tif (flags & CV_EVENT_FLAG_LBUTTON) {\r\n\t\t\tdrawing_box = true;\r\n\t\t\tif ( drawing_box ) {\r\n\t\t\t\tbox.width = x - box.x;\r\n\t\t\t\tbox.height = y - box.y;\r\n\t\t\t}\r\n\t\t}\r\n\t\telse if (flags & CV_EVENT_FLAG_RBUTTON) {\r\n\t\t\tmoving = true;\r\n\t\t}\r\n\tbreak;\r\n\r\n\t\/\/\u5de6\u952e\u6309\u4e0b\r\n\tcase CV_EVENT_LBUTTONDOWN: \r\n\t\tdrawing_box = false;\r\n\t\tbox = cvRect(x,y,0,0);\r\n\t\tbreak;\r\n\r\n\t\/\/\u5de6\u952e\u62ac\u8d77\r\n\tcase CV_EVENT_LBUTTONUP:\r\n\t\tif (drawing_box) {\r\n\t\t\tdrawing_box = false;\r\n\t\t\t\/\/\u786e\u4fdd\u9009\u5b9a\u6846\u957f\u5bbd\u6b63\u503c\r\n\t\t\tif (box.width < 0) {\r\n\t\t\t\tbox.x += box.width;\r\n\t\t\t\tbox.width *= -1;\r\n\t\t\t}\r\n\t\t\tif (box.height < 0) {\r\n\t\t\t\tbox.y += box.height;\r\n\t\t\t\tbox.height *= -1;\r\n\t\t\t}\r\n\t\t\tmove_zoom_image(image, box);\r\n\t\t}\r\n\t\telse {\r\n\t\t\tbox = cvRect(x-IMAGE_SIZE_WID\/2, y-IMAGE_SIZE_HGT\/2, IMAGE_SIZE_WID, IMAGE_SIZE_HGT);\r\n\t\t\tmove_zoom_image(image, box);\r\n\t\t}\r\n\t\tbreak;\r\n\r\n\t\/\/\u53f3\u952e\u6309\u4e0b\r\n\tcase CV_EVENT_RBUTTONDOWN:\r\n\t\tmoving = false;\r\n\t\tmov_x = x;\r\n\t\tmov_y = y;\r\n\t\tbreak;\r\n\r\n\t\/\/\u53f3\u952e\u653e\u5f00\r\n\tcase CV_EVENT_RBUTTONUP: \r\n\t\tif (moving) {\r\n\t\t\tmoving = false;\r\n\t\t\tbox = cvRect(mov_x - x, mov_y - y, IMAGE_SIZE_WID, IMAGE_SIZE_HGT);\r\n\t\t\tmove_zoom_image(image, box, 0);\r\n\t\t}\r\n\t\telse {\r\n\t\t\tbox = cvRect(-IMAGE_SIZE_WID\/2, -IMAGE_SIZE_HGT\/2, IMAGE_SIZE_WID*2, IMAGE_SIZE_HGT*2);\r\n\t\t\tmove_zoom_image(image, box);\r\n\t\t}\r\n\t\tbreak;\r\n\t}\r\n}\r\n<\/pre>\n<pre lang=\"cpp\" file=\"xueyr.cpp\">\r\n\/*\r\n\r\n1100300627 \u859b\u6021\u7136 \u5236\u4f5c\r\n\r\n\u73af\u5883  Visual Studio 2010 + OpenCV 2.1\r\n      Code::Blocks 10.05 + MinGW 4.4.1 + OpenCV 2.1\r\n\r\n\u672c\u673a\u7f16\u8bd1\u3001\u8fd0\u884c\u901a\u8fc7\r\n\r\n\u5b9e\u73b0\u529f\u80fd\uff1aTask 2 ~ Task 6\r\n\r\n\u9644\u52a0\u529f\u80fd\uff1a\u53f3\u952e\u62d6\u52a8\u5e73\u79fb\u3001\u591a\u91cd\u91c7\u6837\u6297\u952f\u9f7f\u3001\u52a8\u753b\u7f29\u653e\u3001\u6731\u5229\u4e9a\u96c6\u3001\u591a\u7ebf\u7a0b\u7ed8\u56fe\u3001\u591a\u7ebf\u7a0b\u641c\u7d22\u590d\u6742\u533a\u57df\u3001\u7528\u6237\u914d\u7f6e\u6587\u4ef6\u7b49\r\n\r\n*\/\r\n\r\n#include <iostream>\t\t\/\/C++\r\n#include <fstream>\r\n#include <sstream>\r\n#include <utility>\t\t\/\/STL\r\n#include <string>\r\n#include <vector>\r\n#include <cmath>\t\t\/\/C\r\n#include <cstring>\r\n#include <ctime>\r\n#include <Windows.h>\t\/\/WIN32\r\n#include <cv.h>\t\t\t\/\/OpenCV\r\n#include <highgui.h>\r\n\r\n#define LIMIT_SQUARE 4\t\t\/\/\u8fed\u4ee3\u9608\u503c(2)\u7684\u5e73\u65b9\r\n\r\n\/\/\u4ee5\u4e0b\u5168\u5c40\u53d8\u91cf\u53ef\u901a\u8fc7\u8bbe\u7f6e\u51fd\u6570\u8bfb\u53d6 config.ini \u8fdb\u884c\u66f4\u6539\r\n\r\nchar name_m[] = \"Mandelbrot\";\t\/\/\u7a97\u53e3\u540d\r\nchar name_j[] = \"Julia\";\r\nchar* window_name = name_m;\r\n\r\ndouble julia_set = false;\t\t\/\/\u5206\u5f62\u79cd\u7c7b\uff1afalse:\u66fc\u5f97\u6ce2\u7f57\u96c6 true:\u6731\u5229\u4e9a\u96c6\r\n\r\n\/\/\u4e3a\u7edf\u4e00\u63a5\u53e3\uff0c\u5b9a\u4e49\uff1a\r\n\/\/\u66fc\u5f97\u6ce2\u7f57\u96c6:\t\tz[n+1] = z[n]^2 + c\r\n\/\/\u6731\u5229\u4e9a\u96c6:\t\tc[n+1] = c[n]^2 + j_c\r\n\r\ndouble j_c_re = -0.726895347709114071439;\t\/\/\u6731\u5229\u4e9a\u96c6\u4e2d j_c \u7684\u5b9e\u90e8\r\ndouble j_c_im = 0.188887129043845954792;\t\/\/\u6731\u5229\u4e9a\u96c6\u4e2d j_c \u7684\u865a\u90e8\r\n\r\nbool ssaa = true;\t\t\t\/\/4x\u591a\u91cd\u91c7\u6837\u6297\u952f\u9f7f\u5f00\u5173\r\nint max_iteration = 300;\t\/\/\u6700\u5927\u8fed\u4ee3\u6b21\u6570\r\nint img_width = 800;\t\t\/\/\u56fe\u50cf\u5bbd\u5ea6\r\nint img_height = 600;\t\t\/\/\u56fe\u50cf\u9ad8\u5ea6\r\nint animation = 20;\t\t\t\/\/\u52a8\u753b\u7f29\u653e\u6548\u679c\u6b65\u6570\r\nint max_zoom = 10;\t\t\t\/\/\u81ea\u52a8\u7f29\u653e\u6700\u5927\u6b21\u6570\r\nint zoom_partition = 4;\t\t\/\/\u81ea\u52a8\u7f29\u653e\u753b\u9762\u6bcf\u8f74\u5206\u533a\u6570\r\nint zoom_delay = 3000;\t\t\/\/\u81ea\u52a8\u7f29\u653e\u5ef6\u8fdf\uff08\u6beb\u79d2\uff09\r\ndouble ctr_re = -0.5, ctr_im = 0;\t\/\/\u56fe\u50cf\u4e2d\u5fc3\u590d\u5e73\u9762\u5750\u6807\r\ndouble img_left = -2;\t\t\t\t\/\/\u56fe\u50cf\u5de6\u8fb9\u7f18\u590d\u5e73\u9762\u5b9e\u8f74\u5750\u6807\r\n\r\nint draw_partition = 8;\t\t\/\/\u7ed8\u56fe\u7ebf\u7a0b\u6570\uff08\u4e00\u822c\u4f1a\u518d\u591a\u51fa\u4e00\u4e2a\uff09\r\nint draw_thread = 0;\t\t\/\/\u7ed8\u56fe\u7ebf\u7a0b\u8fd4\u56de\u8ba1\u6570\r\nint beauty_thread = 0;      \/\/\u533a\u57df\u590d\u6742\u5ea6\u7ebf\u7a0b\u8fd4\u56de\u8ba1\u6570\r\n\r\nIplImage *img, *tmp;\t\t\/\/img:\u4e3b\u4f53\u56fe\u50cf tmp:\u7528\u4e8e\u52a8\u6001\u663e\u793a\u7684\u4e34\u65f6\u56fe\u50cf\r\nCvRect box;\t\t\t\t\t\/\/\u4e34\u65f6\u7ebf\u6846\r\nbool drawing_box = false;\t\/\/\u7ed8\u5236\u7ebf\u6846\u72b6\u6001\r\nCvPoint move_from, move_to;\t\/\/\u53f3\u952e\u5e73\u79fb\u8303\u56f4\r\nbool moving = false;\t\t\/\/\u53f3\u952e\u5e73\u79fb\u72b6\u6001\r\n\r\ndouble pi = acos(-1.0);\t\t\/\/\u4e0a\u8272\u4e09\u89d2\u51fd\u6570\u7528 \u03c0\r\ndouble ratio_r = 30;\t\t\/\/\u4e0a\u8272\u4e09\u89d2\u51fd\u6570\u81ea\u53d8\u91cf\u7cfb\u6570\uff08\u5012\u6570\uff09\r\ndouble ratio_g = 25;\r\ndouble ratio_b = 20;\r\ndouble phase_r = 0;\t\t\t\/\/\u4e0a\u8272\u4e09\u89d2\u51fd\u6570\u521d\u76f8 \/ \u03c0\r\ndouble phase_g = 0;\r\ndouble phase_b = 0.25;\r\n\r\nusing namespace std;\r\nusing namespace cv;\r\n\r\n\/\/\u6d4b\u8bd5C\u662f\u5426\u5c5e\u4e8eM_Set\r\nint xMSetTest(double c_re, double c_im) {\r\n\tdouble z_re = 0, z_im = 0, temp;\r\n    int step = 0;\r\n    while (z_re*z_re + z_im*z_im < LIMIT_SQUARE &#038;&#038; step != max_iteration) {\r\n\t\ttemp = z_re * z_re - z_im * z_im;\r\n\t\tz_im = 2 * z_re * z_im + c_im;\r\n\t\tz_re = temp + c_re;\r\n        step++;\r\n    }\r\n    if (z_re*z_re + z_im*z_im < LIMIT_SQUARE &#038;&#038; step == max_iteration)\r\n\t\treturn 0;\r\n    else return step;\r\n}\r\n\r\n\/\/\u6d4b\u8bd5C\u662f\u5426\u5c5e\u4e8eJ_SET\r\nint xJSetTest(double c_re, double c_im) {\r\n\tdouble temp;\r\n\tint step = 0;\r\n\twhile (c_re*c_re + c_im*c_im < LIMIT_SQUARE &#038;&#038; step != max_iteration) {\r\n\t\ttemp = c_re * c_re - c_im * c_im;\r\n\t\tc_im = 2 * c_re * c_im + j_c_im;\r\n\t\tc_re = temp + j_c_re;\r\n        step++;\r\n    }\r\n\tif (c_re*c_re + c_im*c_im < LIMIT_SQUARE &#038;&#038; step == max_iteration)\r\n\t\treturn 0;\r\n\telse return step;\r\n}\r\n\r\n\/\/\u6d4b\u8bd5\u51fd\u6570\u6307\u9488\uff0c\u7528\u4e8e\u7ed9 M_SET \u4e0e J_SET \u6d4b\u8bd5\u63d0\u4f9b\u540c\u6837\u63a5\u53e3\r\nint (*xSetTest)(double c_re, double c_im) = xMSetTest;\r\n\r\n\/\/\u6574\u578b\u7edd\u5bf9\u503c\r\ninline int xAbs(int x) {\r\n\treturn (x > 0) ? x : -x;\r\n}\r\n\r\n\/\/\u50cf\u7d20\u5750\u6807\u8f6c\u6362\u81f3\u590d\u5e73\u9762\u5750\u6807\uff08\u5b9e\u8f74\uff09\r\ninline double xRe(double x) {\r\n\treturn ctr_re + (2 * x - img_width)\r\n\t\t* (ctr_re - img_left) \/ img_width;\r\n}\r\n\r\n\/\/\u50cf\u7d20\u5750\u6807\u8f6c\u6362\u81f3\u590d\u5e73\u9762\u5750\u6807\uff08\u865a\u8f74\uff09\r\ninline double xIm(double y) {\r\n\treturn ctr_im - (2 * y - img_height)\r\n\t\t* (ctr_re - img_left) \/ img_width;\r\n}\r\n\r\n\/\/\u62f7\u8d1d\u56fe\u50cf\u77e9\u5f62\u533a\u57df\uff08\u5141\u8bb8\u8d8a\u754c\uff0c\u8d8a\u754c\u90e8\u5206\u586b\u9ed1\uff09\r\nIplImage* xCloneImageOver(IplImage* src, CvRect rect) {\r\n\tIplImage *dst = cvCreateImage(cvSize(rect.width, rect.height), src->depth, src->nChannels);\r\n\tcvSetZero(dst);\r\n\tCvRect src_range = rect;\r\n\tCvRect dst_range = cvRect(0, 0, rect.width, rect.height);\r\n\t\/\/\u68c0\u67e5\u56db\u4fa7\u8d8a\u754c\uff0c\u9002\u5f53\u7f29\u5c0f\u590d\u5236\u8303\u56f4\r\n\tif (src_range.x < 0) {\r\n\t\tsrc_range.width += src_range.x;\r\n\t\tdst_range.width += src_range.x;\r\n\t\tdst_range.x -= src_range.x;\r\n\t\tsrc_range.x = 0;\r\n\t}\r\n\tif (src_range.y < 0) {\r\n\t\tsrc_range.height += src_range.y;\r\n\t\tdst_range.height += src_range.y;\r\n\t\tdst_range.y -= src_range.y;\r\n\t\tsrc_range.y = 0;\r\n\t}\r\n\tif (src_range.x + src_range.width > src->width) {\r\n\t\tint diff = src_range.x + src_range.width - src->width;\r\n\t\tsrc_range.width -= diff;\r\n\t\tdst_range.width -= diff;\r\n\t}\r\n\tif (src_range.y + src_range.height > src->height) {\r\n\t\tint diff = src_range.y + src_range.height - src->height;\r\n\t\tsrc_range.height -= diff;\r\n\t\tdst_range.height -= diff;\r\n\t}\r\n\t\/\/\u68c0\u67e5\u5b8c\u5168\u8d8a\u754c\u72b6\u6001\r\n\tif (src_range.height <= 0 || src_range.width <= 0) return dst;\r\n\t\/\/\u590d\u5236\u56fe\u50cf\u5185\u5bb9\r\n\tfor (int i = 0; i < src_range.height; i++) {\r\n\t\tchar* src_ptr = src->imageData + (src_range.y + i) * src->widthStep +\r\n\t\t\tsrc_range.x * (src->depth \/ 8) * src->nChannels;\r\n\t\tchar* dst_ptr = dst->imageData + (dst_range.y + i) * dst->widthStep +\r\n\t\t\tdst_range.x * (src->depth \/ 8) * src->nChannels;\r\n\t\tmemcpy(dst_ptr, src_ptr, src_range.width * (src->depth \/ 8) * src->nChannels);\r\n\t}\r\n\treturn dst;\r\n}\r\n\r\n\/\/\u5b9a\u4e49\u50cf\u7d20\u989c\u8272 & 4xAA\r\nCvScalar xColorAA(double x, double y) {\r\n\tdouble result[4];\r\n\tCvScalar color = cvScalar(0);\r\n\tresult[0] = xSetTest(xRe(x - 0.125), xIm(y - 0.25));\r\n\tresult[1] = xSetTest(xRe(x + 0.25), xIm(y - 0.125));\r\n\tresult[2] = xSetTest(xRe(x + 0.125), xIm(y + 0.25));\r\n\tresult[3] = xSetTest(xRe(x - 0.25), xIm(y + 0.125));\r\n\tfor (int i=0; i<4; i++) {\r\n\t\tif (result[i]) {\r\n\t\t\tcolor.val[0] += 127.5 - 127.5 * cos(result[i] \/ ratio_b + phase_b * pi);\r\n\t\t\tcolor.val[1] += 127.5 - 127.5 * cos(result[i] \/ ratio_g + phase_g * pi);\r\n\t\t\tcolor.val[2] += 127.5 - 127.5 * cos(result[i] \/ ratio_r + phase_r * pi);\r\n\t\t} else {\r\n\t\t\tcolor.val[0] += 0;\r\n\t\t\tcolor.val[1] += 0;\r\n\t\t\tcolor.val[2] += 0;\r\n\t\t}\r\n\t}\r\n\tcolor.val[0] \/= 4;\r\n\tcolor.val[1] \/= 4;\r\n\tcolor.val[2] \/= 4;\r\n\treturn color;\r\n}\r\n\r\n\/\/\u5b9a\u4e49\u50cf\u7d20\u989c\u8272 &#038; noAA\r\nCvScalar xColorNoAA(double x, double y) {\r\n\tdouble result = xSetTest(xRe(x),xIm(y));\r\n\tCvScalar color;\r\n\tif (result) {\r\n\t\tcolor.val[0] = 127.5 - 127.5 * cos(result \/ ratio_b + phase_b * pi);\r\n\t\tcolor.val[1] = 127.5 - 127.5 * cos(result \/ ratio_g + phase_g * pi);\r\n\t\tcolor.val[2] = 127.5 - 127.5 * cos(result \/ ratio_r + phase_r * pi);\r\n\t} else {\r\n\t\tcolor.val[0] = 0;\r\n\t\tcolor.val[1] = 0;\r\n\t\tcolor.val[2] = 0;\r\n\t}\r\n\treturn color;\r\n}\r\n\r\n\/\/\u5b9a\u4e49\u50cf\u7d20\u989c\u8272\u51fd\u6570\u6307\u9488\uff0c\u7528\u4e8e\u7ed9 xColorAA \u548c xColorNoAA \u63d0\u4f9b\u540c\u6837\u63a5\u53e3\r\nCvScalar (*xColor)(double x, double y) = xColorNoAA;\r\n\r\n\/\/\u5355\u4e2a\u7ed8\u5236\u56fe\u50cf\u7ebf\u7a0b\r\nDWORD WINAPI xDrawThread(LPVOID param) {\r\n\tpair<int,int> *range = (pair<int,int>*)param;\r\n\tfor (int i=range->first; i<range->second; i++) {\r\n\t\tfor (int j=0; j<img_width; j++)\r\n\t\t\tcvSet2D(img, i, j, xColor(j, i));\r\n\t}\r\n\tdraw_thread--;\r\n\treturn 0;\r\n}\r\n\r\n\/\/\u6839\u636e\u53c2\u6570\u7ed8\u5236\u5f53\u524d\u56fe\u50cf\r\nvoid xReDraw(){\r\n\tvector< pair<int,int> > param(draw_partition + 1);\r\n\tdraw_thread = 0;\r\n\t\/\/\u591a\u7ebf\u7a0b\u7ed8\u5236\r\n\tfor (int i=0; i<draw_partition; i++) {\r\n\t\tparam[i].first = i * (img_height \/ draw_partition);\r\n\t\tparam[i].second = (i + 1) * (img_height \/ draw_partition);\r\n\t\tif (param[i].first != param[i].second) {\r\n\t\t\tCreateThread(NULL, 0, xDrawThread, &#038;param[i], 0, NULL);\r\n\t\t\tdraw_thread++;\r\n\t\t}\r\n\t}\r\n\t\/\/\u7ed8\u5236\u5269\u4f59\u90e8\u5206\r\n\tif ((img_height \/ draw_partition) * draw_partition < img_height) {\r\n\t\tparam[draw_partition].first = (img_height \/ draw_partition) * draw_partition;\r\n\t\tparam[draw_partition].second = img_height;\r\n\t\tCreateThread(NULL, 0, xDrawThread, &#038;param[draw_partition], 0, NULL);\r\n\t\tdraw_thread++;\r\n\t}\r\n\tfor (;;) {\r\n\t\tSleep(20);\r\n\t\tif (draw_thread == 0) return;\r\n\t}\r\n}\r\n\r\n\/\/\u4ee5\u77e9\u5f62\u50cf\u7d20\u5750\u6807\u7f29\u653e\r\nvoid xZoom(CvRect rect, double zoom = 0) {\r\n\tint t_ctr_x = rect.x + rect.width \/ 2;\r\n\tint t_ctr_y = rect.y + rect.height \/ 2;\r\n\tint t_left = zoom ? (int)(t_ctr_x - img_width \/ zoom \/ 2) : rect.x;\r\n\tint t_top = zoom ? (int)(t_ctr_y - img_height \/ zoom \/ 2) : rect.y;\r\n\tCvRect t_rect;\r\n\t\/\/\u7f29\u653e\u52a8\u753b\u6548\u679c\r\n\tfor (int i = 0; i <= animation; i++) {\r\n\t\tt_rect = cvRect(\r\n\t\t\ti * t_left \/ animation,\r\n\t\t\ti * t_top \/ animation,\r\n\t\t\t(i * 2 * (t_ctr_x - t_left) + (animation - i) * img_width) \/ animation,\r\n\t\t\t(i * 2 * (t_ctr_y - t_top) + (animation - i) * img_height) \/ animation);\r\n\t\tIplImage *piece = xCloneImageOver(img, t_rect);\r\n\t\tcvResize(piece, tmp);\r\n\t\tcvReleaseImage(&#038;piece);\r\n\t\tcvShowImage(window_name, tmp);\r\n\t\tcvWaitKey(5);\r\n\t}\r\n\t\/\/\u66f4\u6539\u590d\u5e73\u9762\u53c2\u6570\u5e76\u91cd\u7ed8\r\n\tdouble t_ctr_re = xRe(t_ctr_x);\r\n\tdouble t_ctr_im = xIm(t_ctr_y);\r\n\timg_left = xRe(t_left);\r\n\tctr_re = t_ctr_re;\r\n\tctr_im = t_ctr_im;\r\n\txReDraw();\r\n}\r\n\r\n\/\/\u9f20\u6807\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\r\nvoid xOnMouse(int m_event, int x, int y, int flags, void* param) {\r\n\tswitch (m_event) {\r\n\tcase CV_EVENT_MOUSEMOVE: \/\/\u9f20\u6807\u79fb\u52a8\r\n\t\tif (x > 10000) x = 0;\r\n\t\tif (y > 10000) y = 0;\r\n\t\tif (flags & CV_EVENT_FLAG_LBUTTON) { \/\/\u5de6\u952e\u62d6\u52a8\r\n\t\t\tdrawing_box = true;\r\n\t\t\tbox.width = x - box.x;\r\n\t\t\tbox.height = y - box.y;\r\n\t\t\tif (xAbs(box.width) * img_height > xAbs(box.height) * img_width) {\r\n\t\t\t\tbox.height = ((box.height > 0) ? 1 : -1) *\r\n\t\t\t\t\txAbs(box.width) * img_height \/ img_width;\r\n\t\t\t} else {\r\n\t\t\t\tbox.width = ((box.width > 0) ? 1 : -1) *\r\n\t\t\t\t\txAbs(box.height) * img_width \/ img_height;\r\n\t\t\t}\r\n\t\t} else if (flags & CV_EVENT_FLAG_RBUTTON) { \/\/\u53f3\u952e\u62d6\u52a8\r\n\t\t\tmoving = true;\r\n\t\t\tmove_to.x = 2 * move_from.x - x;\r\n\t\t\tmove_to.y = 2 * move_from.y - y;\r\n\t\t}\r\n\t\tbreak;\r\n\tcase CV_EVENT_LBUTTONDOWN: \/\/\u5de6\u952e\u6309\u4e0b\r\n\t\tdrawing_box = false;\r\n\t\tbox = cvRect(x,y,0,0);\r\n\t\tbreak;\r\n\tcase CV_EVENT_LBUTTONUP: \/\/\u5de6\u952e\u653e\u5f00\r\n\t\tif (drawing_box && xAbs(box.width) > 2) {\r\n\t\t\tdrawing_box = false;\r\n\t\t\tif (box.width < 0) {\r\n\t\t\t\tbox.x += box.width;\r\n\t\t\t\tbox.width *= -1;\r\n\t\t\t}\r\n\t\t\tif (box.height < 0) {\r\n\t\t\t\tbox.y += box.height;\r\n\t\t\t\tbox.height *= -1;\r\n\t\t\t}\r\n\t\t\txZoom(box);\r\n\t\t} else {\r\n\t\t\tdrawing_box = false;\r\n\t\t\txZoom(cvRect(x - img_width \/ 2, y - img_height \/ 2,\r\n\t\t\t\timg_width, img_height));\r\n\t\t}\r\n\t\tbreak;\r\n\tcase CV_EVENT_RBUTTONDOWN: \/\/\u53f3\u952e\u6309\u4e0b\r\n\t\tmoving = false;\r\n\t\tmove_from.x = x;\r\n\t\tmove_from.y = y;\r\n\t\tbreak;\r\n\tcase CV_EVENT_RBUTTONUP: \/\/\u53f3\u952e\u653e\u5f00\r\n\t\tif (moving) {\r\n\t\t\tmoving = false;\r\n\t\t\tdouble t_ctr_re = xRe(move_to.x - move_from.x + img_width \/ 2.0);\r\n\t\t\tdouble t_ctr_im = xIm(move_to.y - move_from.y + img_height \/ 2.0);\r\n\t\t\timg_left = xRe(move_to.x - move_from.x);\r\n\t\t\tctr_re = t_ctr_re;\r\n\t\t\tctr_im = t_ctr_im;\r\n\t\t\txReDraw();\r\n\t\t} else {\r\n\t\t\txZoom(cvRect(x, y, 0, 0), 0.5);\r\n\t\t}\r\n\t\tbreak;\r\n\t}\r\n}\r\n\r\n\/\/\u8ba1\u7b97\u533a\u57df\u590d\u6742\u5ea6\u7ebf\u7a0b\u53c2\u6570\r\nstruct BeautyParam {\r\n\tdouble result;\r\n\tCvPoint begin, end;\r\n};\r\n\r\n\/\/\u8ba1\u7b97\u533a\u57df\u590d\u6742\u5ea6\u7ebf\u7a0b\r\nDWORD WINAPI xBeautyThread(LPVOID thread_param) {\r\n\tBeautyParam *param = (BeautyParam*)thread_param;\r\n\tint real_num = (param->end.x - param->begin.x) * (param->end.y - param->begin.y);\r\n\tint color_num = real_num;\r\n\tCvScalar average = cvScalar(0);\r\n\tparam->result = 0;\r\n\tuchar *ptr;\r\n\t\/\/\u8ba1\u7b97\u5e73\u5747\u503c\uff0c\u9ed1\u8272\u4e0d\u8ba1\r\n\tfor (int i=param->begin.y; i<param->end.y; i++) {\r\n\t\tptr = (uchar*)(img->imageData + i * img->widthStep);\r\n\t\tfor (int j=param->begin.x; j<param->end.x; j++) {\r\n\t\t\tif (ptr[3 * j] || ptr[3 * j + 1] || ptr[3 * j + 2]) {\r\n\t\t\t\taverage.val[0] += ptr[3 * j];\r\n\t\t\t\taverage.val[1] += ptr[3 * j + 1];\r\n\t\t\t\taverage.val[2] += ptr[3 * j + 2];\r\n\t\t\t} else {\r\n\t\t\t\tcolor_num--;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tif (color_num) {\r\n\t\taverage.val[0] \/= color_num;\r\n\t\taverage.val[1] \/= color_num;\r\n\t\taverage.val[2] \/= color_num;\r\n\t}\r\n\t\/\/\u8ba1\u7b97\u65b9\u5dee\uff0c\u9ed1\u8272\u4e0d\u8ba1\u5165\u5e73\u65b9\u548c\uff0c\u4f46\u8ba1\u5165\u5206\u6bcd\u6570\r\n\tfor (int i=param->begin.y; i<param->end.y; i++) {\r\n\t\tptr = (uchar*)(img->imageData + i * img->widthStep);\r\n\t\tfor (int j=param->begin.x; j<param->end.x; j++) {\r\n\t\t\tif (ptr[3 * j] || ptr[3 * j + 1] || ptr[3 * j + 2]) {\r\n\t\t\t\tparam->result += (ptr[3 * j] - average.val[0]) *\r\n\t\t\t\t\t(ptr[3 * j] - average.val[0]);\r\n\t\t\t\tparam->result += (ptr[3 * j + 1] - average.val[1]) *\r\n\t\t\t\t\t(ptr[3 * j + 1] - average.val[1]);\r\n\t\t\t\tparam->result += (ptr[3 * j + 2] - average.val[2]) *\r\n\t\t\t\t\t(ptr[3 * j + 2] - average.val[2]);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tparam->result \/= real_num;\r\n\tbeauty_thread--;\r\n\treturn 0;\r\n}\r\n\r\n\/\/\u67e5\u627e\u590d\u6742\u533a\u57df\r\nint xFindBeauty(){\r\n\tvector<BeautyParam> param(zoom_partition * zoom_partition);\r\n\tint unit_x = img_width \/ zoom_partition;\r\n\tint unit_y = img_height \/ zoom_partition;\r\n\tCvRNG seed = cvRNG(time(0));\r\n\t\/\/\u56fe\u50cf\u5206\u533a\uff0c\u591a\u7ebf\u7a0b\u8ba1\u7b97\u65b9\u5dee\r\n\tbeauty_thread = 0;\r\n\tfor (int i=0; i<zoom_partition; i++) {\r\n\t\tfor (int j=0; j<zoom_partition; j++) {\r\n\t\t\tparam[i * zoom_partition + j].begin = cvPoint(j * unit_x, i * unit_y);\r\n\t\t\tparam[i * zoom_partition + j].end = cvPoint((j + 1) * unit_x, (i + 1) * unit_y);\r\n\t\t\tCreateThread(NULL, 0, xBeautyThread, &#038;param[i * zoom_partition + j], 0, NULL);\r\n\t\t\tbeauty_thread++;\r\n\t\t}\r\n\t}\r\n\tfor (;;) {\r\n\t\tSleep(20);\r\n\t\tif (beauty_thread == 0) break;\r\n\t}\r\n\t\/\/\u67e5\u627e\u65b9\u5dee\u6700\u5927\u533a\u57df\uff0c\u9002\u5f53\u52a0\u5165\u968f\u673a\u6570\r\n\tint max = 0;\r\n\tfor (int i=0; i < zoom_partition * zoom_partition; i++) {\r\n\t\tparam[i].result *= cvRandReal(&#038;seed) \/ 2 + 1;\r\n\t\tif (param[i].result > param[max].result) max = i;\r\n\t}\r\n\treturn max;\r\n};\r\n\r\n\/\/\u81ea\u52a8\u67e5\u627e\u590d\u6742\u533a\u57df\u5e76\u7f29\u653e\r\nvoid xAutoZoom() {\r\n\tint zoom_to;\r\n\tint unit_x = img_width \/ zoom_partition;\r\n\tint unit_y = img_height \/ zoom_partition;\r\n\tCvRect auto_box;\r\n\tcout << \"Auto Zoom\" << endl;\r\n\tfor (int i=0; i<max_zoom; i++) {\r\n\t\tif (cvWaitKey(20) == 27) break;\r\n\t\t\/\/\u67e5\u627e\u590d\u6742\u533a\u57df\r\n\t\tcout << \"  auto zoom step \" << i + 1 << \" of \" << max_zoom;\r\n\t\tzoom_to = xFindBeauty();\r\n\t\t\/\/\u7ed8\u5236\u77e9\u5f62\u6846\uff0c\u6307\u793a\u67e5\u627e\u7ed3\u679c\r\n\t\tauto_box = cvRect(zoom_to % zoom_partition * unit_x, zoom_to \/ zoom_partition * unit_y,\r\n\t\t\tunit_x, unit_y);\r\n\t\tcvCopyImage(img, tmp);\r\n\t\tcvRectangle(tmp, cvPoint(auto_box.x, auto_box.y),\r\n\t\t\tcvPoint(auto_box.x + auto_box.width, auto_box.y + auto_box.height),\r\n\t\t\tcvScalar(0xff,0xff,0xff), 2);\r\n\t\tcvRectangle(tmp, cvPoint(auto_box.x, auto_box.y),\r\n\t\t\tcvPoint(auto_box.x + auto_box.width, auto_box.y + auto_box.height),\r\n\t\t\tcvScalar(0x00,0x00,0xff));\r\n\t\tcvShowImage(window_name, tmp);\r\n\t\tcout << \" ... \";\r\n\t\tif (cvWaitKey(zoom_delay \/ 4) == 27){\r\n\t\t\tcvShowImage(window_name, img);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\t\/\/\u52a8\u753b\u7f29\u653e\u81f3\u67e5\u627e\u7ed3\u679c\u533a\u57df\r\n\t\txZoom(auto_box);\r\n\t\tcvShowImage(window_name, img);\r\n\t\tcout << \"ok\" << endl;\r\n\t\tif (i != max_zoom - 1) {\r\n\t\t\tif (cvWaitKey(zoom_delay * 3 \/ 4) == 27) break;\r\n\t\t}\r\n\t}\r\n\t\/\/\u8fd4\u56de\u624b\u52a8\u64cd\u4f5c\u65f6\u7ed8\u5236\u5168\u5c4f\u7ebf\u6846\r\n\tif (cvWaitKey(200) == 27){\r\n\t\tcout << \"Manual Operate\" << endl;\r\n\t\treturn;\r\n\t}\r\n\tif (img_width >= 40 && img_height >= 40){\r\n\t\tcvCopyImage(img, tmp);\r\n\t\tcvRectangle(tmp, cvPoint(20, 20),\r\n\t\t\tcvPoint(img_width - 20, img_height - 20),\r\n\t\t\tcvScalar(0xff,0xff,0xff), 2);\r\n\t\tcvRectangle(tmp, cvPoint(20, 20),\r\n\t\t\tcvPoint(img_width - 20, img_height - 20),\r\n\t\t\tcvScalar(0xff,0x00,0x00));\r\n\t\tcvShowImage(window_name, tmp);\r\n\t\tcvWaitKey(1000);\r\n\t\tcvShowImage(window_name, img);\r\n\t}\r\n\tcout << \"Manual Operate\" << endl;\r\n}\r\n\r\n\/\/\u5faa\u73af\u7ed8\u5236\u56fe\u50cf\u5e76\u63a5\u6536\u7528\u6237\u8f93\u5165\r\nvoid xLoop() {\r\n\tbox = cvRect(-1,-1,0,0);\r\n\tint key = 0;\r\n\txReDraw();\r\n\tfor (;;) {\r\n\t\tif (drawing_box) { \/\/\u5de6\u952e\u9009\u5b9a\u77e9\u5f62\u533a\u57df\u65f6\r\n\t\t\tcvCopyImage(img,tmp);\r\n\t\t\tcvRectangle(tmp,\r\n\t\t\t\tcvPoint(box.x, box.y),\r\n\t\t\t\tcvPoint(box.x + box.width, box.y + box.height),\r\n\t\t\t\tcvScalar(0xff,0xff,0xff), 2);\r\n\t\t\tcvRectangle(tmp,\r\n\t\t\t\tcvPoint(box.x, box.y),\r\n\t\t\t\tcvPoint(box.x + box.width, box.y + box.height),\r\n\t\t\t\tcvScalar(0xff,0x00,0x00));\r\n\t\t\tcvShowImage(window_name,tmp);\r\n\t\t} else if (moving) { \/\/\u53f3\u952e\u62d6\u52a8\u5e73\u79fb\u56fe\u50cf\u65f6\r\n\t\t\tIplImage *move = xCloneImageOver(img, cvRect(\r\n\t\t\t\tmove_to.x - move_from.x,\r\n\t\t\t\tmove_to.y - move_from.y,\r\n\t\t\t\timg_width,\r\n\t\t\t\timg_height));\r\n\t\t\tcvShowImage(window_name, move);\r\n\t\t\tcvReleaseImage(&#038;move);\r\n\t\t} else { \/\/\u7a7a\u95f2\u65f6\r\n\t\t\tcvShowImage(window_name, img);\r\n\t\t}\r\n\t\tcvSetMouseCallback(window_name, xOnMouse);\r\n\t\tif (drawing_box || moving) key = cvWaitKey(15);\r\n\t\telse key = cvWaitKey(100);\r\n\t\tif (key == 27) return;\r\n\t\tif (key == 'a') xAutoZoom();\r\n\t}\r\n}\r\n\r\n\/\/\u8bfb\u53d6\u8bbe\u7f6e\u6587\u4ef6\r\nvoid xSetting() {\r\n\tifstream fi(\"config.ini\");\r\n\tstring cmd;\r\n\tdouble val;\r\n\t\/\/\u968f\u673a\u751f\u6210\u56fe\u50cf\u989c\u8272\r\n\tCvRNG seed = cvRNG(time(0));\r\n\tratio_r = cvRandReal(&#038;seed) * 50 + 10;\r\n\tratio_g = cvRandReal(&#038;seed) * 50 + 10;\r\n\tratio_b = cvRandReal(&#038;seed) * 50 + 10;\r\n\tphase_r = cvRandReal(&#038;seed) * 2 * pi;\r\n\tphase_g = cvRandReal(&#038;seed) * 2 * pi;\r\n\tphase_b = cvRandReal(&#038;seed) * 2 * pi;\r\n\t\/\/\u8bfb\u53d6\u7528\u6237\u8bbe\u7f6e\r\n\twhile (fi >> cmd >> val) {\r\n\t\tif (cmd == \"julia_set\" && val == 1) {\r\n\t\t\tjulia_set = true;\r\n\t\t\twindow_name = name_j;\r\n\t\t\txSetTest = xJSetTest;\r\n\t\t\tctr_re = 0;\r\n\t\t\tctr_im = 0;\r\n\t\t\timg_left = -1.5;\r\n\t\t}\r\n\t\telse if (cmd == \"ssaa\" || cmd == \"4xAA\") ssaa = (val != 0);\r\n\t\telse if (cmd == \"max_iteration\") max_iteration = (int)val;\r\n\t\telse if (cmd == \"img_width\" && val > 0) img_width = (int)val;\r\n\t\telse if (cmd == \"img_height\" && val > 0) img_height = (int)val;\r\n\t\telse if (cmd == \"animation\" && val >= 1) animation = (int)val;\r\n\t\telse if (cmd == \"max_zoom\" && val >= 1) max_zoom = (int)val;\r\n\t\telse if (cmd == \"zoom_partition\" && val >= 1) zoom_partition = (int)val;\r\n\t\telse if (cmd == \"zoom_delay\" && val >= 1) zoom_delay = (int)val;\r\n\t\telse if (cmd == \"draw_partition\" && val >= 1) draw_partition = (int)val;\r\n\t\telse if (cmd == \"ctr_re\") ctr_re = val;\r\n\t\telse if (cmd == \"ctr_im\") ctr_im = val;\r\n\t\telse if (cmd == \"img_left\") img_left = val;\r\n\t\telse if (cmd == \"ratio_r\" && val != 0) ratio_r = val;\r\n\t\telse if (cmd == \"ratio_g\" && val != 0) ratio_g = val;\r\n\t\telse if (cmd == \"ratio_b\" && val != 0) ratio_b = val;\r\n\t\telse if (cmd == \"phase_r\") phase_r = val;\r\n\t\telse if (cmd == \"phase_r\") phase_r = val;\r\n\t\telse if (cmd == \"phase_r\") phase_r = val;\r\n\t\telse if (cmd == \"j_c_re\") j_c_re = val;\r\n\t\telse if (cmd == \"j_c_im\") j_c_im = val;\r\n\t}\r\n\t\/\/\u68c0\u67e5\u590d\u5e73\u9762\u8bbe\u7f6e\r\n\tif (img_left > ctr_re) img_left = 2 * ctr_re - img_left;\r\n\t\/\/\u8bbe\u7f6e\u8ba1\u7b97\u50cf\u7d20\u989c\u8272\u51fd\u6570\u6307\u9488\r\n\tif (ssaa) xColor = xColorAA;\r\n}\r\n\r\n\/\/\u521b\u5efa\u548c\u5220\u9664\u56fe\u50cf\r\nint main() {\r\n\txSetting();\r\n\tcout << \"Use mouse to zoom in, zoom out, or move the image.\" << endl;\r\n\tcout << \"Press A in the image window to enter Auto Zoom mode.\" << endl << endl;\r\n\timg = cvCreateImage(cvSize(img_width, img_height),\/*depth*\/8,\/*nChannels*\/3);\r\n\ttmp = cvCloneImage(img);\r\n\txLoop();\r\n\tcvReleaseImage(&#038;img);\r\n\tcvReleaseImage(&#038;tmp);\r\n\treturn 0;\r\n}\r\n<\/pre>\n<h3>\u53c2\u8003\u8d44\u6599<\/h3>\n<p><a href=\" http:\/\/www.opencv.org.cn\/index.php\/%E9%A6%96%E9%A1%B5\">OpenCV \u4e2d\u6587\u7ad9<\/a><br \/>\n<a href=\"http:\/\/www.matrix67.com\/blog\/archives\/292\">M67 Pascal \u7684\u7ed8\u56fe\u7a0b\u5e8f..<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nov 29th\uff0c\u53bb\u4e86\u4e00\u4e0b\u697c\u4e0a\u5bdd\u5ba4\u819c\u62dc\u4e86\u4e00\u4e0b\u6c99\u6f20\u541b\uff0c\u5f97\u5230\u4e86\u5b83\u4eec\u76ee\u524d\u7684\u5de5\u4f5c\u8fdb\u5c55\uff0c\u662f\u4e00\u4efd\u9ad8\u7ea7\u7a0b\u5e8f\u8bed\u8a00\u7684\u4f5c\u4e1a &#8230; \u554a\uff0c\u5f53\u65f6\u6211\u5c31\u60ca\u4e86&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}