gpt4 book ai didi

python - 灰度图像到等温线轮廓

转载 作者:行者123 更新时间:2023-12-02 16:24:36 25 4
gpt4 key购买 nike

我有灰度图像并想用等温线转换为强度轮廓,在我的代码中我只得到一个轮廓以及如何应用等温线?
目标:
enter image description here

import numpy as np
import cv2 as cv
img = cv2.imread(path)
imgray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

cv.drawContours(img, contours, -1, (0,255,0), 3)

plt.imshow(img)
enter image description here

最佳答案

你在正确的轨道上,你所要做的就是接受 127你硬编码到代码中,并迭代几个不同的值。因此,拿起你所拥有的,添加一些东西(包括 viridis colormap 的插头):

import numpy as np
import cv2

# I don't have your image, so I will just create a similar one.
H, W = 480, 640
img = np.zeros([H, W, 3], dtype=np.uint8)
cv2.circle(img, (W//2, H//2), 200, (255,255,255), -1)
img = cv2.GaussianBlur(img, (551, 551), 0)
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# The viridis colormap is better than the jet one you have used.
img_viridis = cv2.applyColorMap(imgray, cv2.COLORMAP_VIRIDIS)

# This for-loop allows you to draw isotherm lines at any value you want.
THRESHES = [30, 90, 170]
for val in THRESHES:
ret, thresh = cv2.threshold(imgray, val, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img_viridis, contours, -1, (0, 0, 255), 2)

cv2.imshow('img', img_viridis)
k = cv2.waitKey(0)

输出:
enter image description here

关于python - 灰度图像到等温线轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62291786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com