本文实例为大家分享了opencv实现回形遍历像素算法的具体代码,供大家参考,具体内容如下
代码实现
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
|
# -*- coding:utf-8 -*- import cv2 import numpy as np cv2.namedwindow( 'img' , 0 ) def traversepixelbycycloidline(image): """ 从一副灰度图像的中心开始向边缘按回形线的方式遍历所有像素,每个像素只能访问一次。 我目前实现了基本的算法, 但存在以下问题: 1) 只支持方阵, 且行列为奇数 2) 只实现, 代码没整理 """ h, w = image.shape[: 2 ] assert h = = w and h % 2 = = 1 , '只支持方阵, 且行列为奇数' center_x, center_y = [w / / 2 , h / / 2 ] traverse_num = h * w cycloid_num = 0 value = 1 while true: for i in range (cycloid_num * 2 + 1 ): if value > = traverse_num: return image center_x = center_x + 1 image[center_y, center_x] = 255 value + = 1 cv2.imshow( 'img' , image) cv2.waitkey( 33 ) for i in range (cycloid_num * 2 + 1 ): if value > = traverse_num: return image center_y = center_y + 1 image[center_y, center_x] = 255 value + = 1 cv2.imshow( 'img' , image) cv2.waitkey( 33 ) for i in range (cycloid_num * 2 + 2 ): if value > = traverse_num: return image center_x = center_x - 1 image[center_y, center_x] = 255 value + = 1 cv2.imshow( 'img' , image) cv2.waitkey( 33 ) for i in range (cycloid_num * 2 + 2 ): if value > = traverse_num: return image center_y = center_y - 1 image[center_y, center_x] = 255 value + = 1 cv2.imshow( 'img' , image) cv2.waitkey( 33 ) cycloid_num + = 1 image_wh = 11 while true: image = np.zeros((image_wh, image_wh, 3 ), dtype = np.uint8) traversepixelbycycloidline(image) |
效果展示
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u014072827/article/details/114373552