本文實(shí)例為大家分享了opencv實(shí)現(xiàn)回形遍歷像素算法的具體代碼,供大家參考,具體內(nèi)容如下
代碼實(shí)現(xiàn)
# -*- coding:utf-8 -*- import cv2 import numpy as np cv2.namedWindow('img', 0) def traversePixelByCycloidLine(image): """ 從一副灰度圖像的中心開(kāi)始向邊緣按回形線的方式遍歷所有像素,每個(gè)像素只能訪問(wèn)一次。 我目前實(shí)現(xiàn)了基本的算法, 但存在以下問(wèn)題: 1) 只支持方陣, 且行列為奇數(shù) 2) 只實(shí)現(xiàn), 代碼沒(méi)整理 """ h, w = image.shape[:2] assert h == w and h % 2 == 1, '只支持方陣, 且行列為奇數(shù)' 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)
效果展示
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:懷化 昆明 西寧 文山 浙江 梅州 錫林郭勒盟 石家莊
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《opencv實(shí)現(xiàn)回形遍歷像素算法》,本文關(guān)鍵詞 opencv,實(shí)現(xiàn),回形,遍歷,像素,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。