You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用OpenCV定位图片中的化学检测试纸?Canny检测未生成目标Bounding Box

问题描述

我有一张包含化学检测试纸的图片,希望通过边缘检测为其绘制Bounding Box,但我的Python代码未能生成预期的框,不确定是未检测到目标还是绘制逻辑有误。

原始检测试纸图片

尝试代码

import cv2
import numpy as np

img = cv2.imread("image1.jpg")
(B, G, R) = cv2.split(img)
img = cv2.Canny(B, 20, 100)   # Blue channel gives the best box so far
# img = cv2.Canny(R, 20, 100)
# img = cv2.Canny(R, 20, 100)


ret,thresh = cv2.threshold(img,20,100,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)


cnt = contours[0]
M = cv2.moments(cnt)

for c in contours:
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box = np.intp(box)
    img = cv2.drawContours(img,[box],0,(0,0,255),2)



# display the image with bounding rectangle drawn on it 
# cv2.namedWindow('Bounding Rectangle', cv2.WINDOW_KEEPRATIO)
cv2.imshow('Bounding Rectangle', img) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 

当前输出效果

当前代码生成的图片

预期效果

预期生成的图片效果


内容的提问来源于stack exchange,提问作者THESIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:50:20