亲宝软件园·资讯

展开

opencv回形遍历像素算法 opencv实现回形遍历像素算法

ancy_i_cv 人气:0
想了解opencv实现回形遍历像素算法的相关内容吗,ancy_i_cv在本文为您仔细讲解opencv回形遍历像素算法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:opencv回形遍历像素算法,opencv回形遍历像素算法,opencv像素算法,下面大家一起来学习吧。

代码实现

# -*- 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)

效果展示

加载全部内容

相关教程
猜你喜欢
用户评论