ana*_*nad 2 python opencv image-processing scikit-image
这就是我想在 python 中做的事情:首先,给出图像上的一个特定点(以红色显示),我想将其径向分割成任意给定数量的等距部分。然后我想一次选择每个部分中的像素强度(以蓝色显示)。任何帮助将非常感激。
不要将背景绘制为白色并用红色或蓝色填充该区域,而是将背景绘制为黑色并用白色填充该区域。然后,您可以使用乘法或按位与来根据扇区掩码选择真实图像中的像素。
\n现在您只需一次绘制一个扇区,并用白色填充。你知道中心,所以你只需要另外 2 个点来构建三角形。由于任何图像中可能的最长直线是对角线,因此计算其长度。现在,使用它作为从中心发出的每条径向线的长度,您可以使用简单的三角学来计算这两条线,因为您知道它们之间的角度为 360/N,其中 N 是扇区数。不要担心您计算图像边缘之外的两个顶点,所有库都允许您这样做。
\n这是一些代码 - 没有经过太彻底的测试 - 但你明白了:
\n#!/usr/bin/env python3\n\nimport cv2\nimport math\nimport numpy as np\n\nh, w = 600, 1200 # image height and width\ncx, cy = 200, 300 # (x,y) coordinates of circle centre\nN = 16 # number of slices in our pie\nl = h + w # length of radial lines - larger than necessary\n\n# Create each sector in white on black background\nfor sector in range(N):\n startAngle = sector * 360/N\n endAngle = startAngle + 360/N\n x1 = cx + l * math.sin(math.radians(startAngle))\n y1 = cy - l * math.cos(math.radians(startAngle))\n x2 = cx + l * math.sin(math.radians(endAngle))\n y2 = cy - l * math.cos(math.radians(endAngle))\n vertices = [(cy, cx), (y1, x1), (y2, x2)]\n print(f\'DEBUG: sector={sector}, startAngle={startAngle}, endAngle={endAngle}\')\n # Make empty black canvas\n im = np.zeros((h,w), np.uint8)\n #\xc2\xa0Draw this pie slice in white\n cv2.fillPoly(im, np.array([vertices],\'int32\'), 255)\n cv2.imwrite(f\'DEBUG-{sector:03d}.png\', im)\n cv2.imshow(\'title\',im)\n cv2.waitKey(0)\nRun Code Online (Sandbox Code Playgroud)\n\n如果您从这张图片开始:
\n\n并将代码更改为:
\n#!/usr/bin/env python3\n\nimport cv2\nimport math\nimport numpy as np\n\norig = cv2.imread(\'artistic-swirl.jpg\', cv2.IMREAD_ANYCOLOR)\nprint(orig.shape)\n\nh, w = 600, 1200 # image height and width\ncx, cy = 200, 300 # (x,y) coordinates of circle centre\nN = 16 # number of slices in our pie\nl = h + w # length of radial lines - larger than necessary\n\n# Create each sector in white on black background\nfor sector in range(N):\n startAngle = sector * 360/N\n endAngle = startAngle + 360/N\n x1 = cx + l * math.sin(math.radians(startAngle))\n y1 = cy - l * math.cos(math.radians(startAngle))\n x2 = cx + l * math.sin(math.radians(endAngle))\n y2 = cy - l * math.cos(math.radians(endAngle))\n vertices = [(cy, cx), (y1, x1), (y2, x2)]\n print(f\'DEBUG: sector={sector}, startAngle={startAngle}, endAngle={endAngle}\')\n # Make empty black canvas\n im = np.zeros((h,w), np.uint8)\n #\xc2\xa0Draw this pie slice in white\n cv2.fillPoly(im, np.array([vertices],\'int32\'), 255)\n cv2.imwrite(f\'DEBUG-{sector:03d}.png\', im)\n #cv2.imshow(\'title\',im)\n #cv2.waitKey(0)\n mask = np.dstack((im,im,im))\n res = cv2.bitwise_and(mask,orig)\n cv2.imshow(\'image\',res)\n cv2.waitKey(0)\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |