首页 > 解决方案 > 无法使用 opencv 读取 .tdms 文件

问题描述

我需要使用 opencv 进行图像检测。输入文件来自传感器,正在尝试识别矩形。输入文件为 .tdms 格式。我无法为此使用opencv。

img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/modules/imgproc/src/color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'

from nptdms import TdmsFile as td 
import numpy as np
import cv2
from matplotlib import pyplot as plt

file = 'source.tdms' 
with td.open(file) as tdms_file:
 img = tdms_file.as_dataframe()
#use only intesting part of image
clip=img.iloc[700:1250,450:1550:]

img_rgb = np.array(clip,dtype=int)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('smallrectangle.jpeg', 0)

height, width = template.shape[::]

res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
plt.imshow(res, cmap='gray')

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

top_left = min_loc  #Change to max_loc for all except for TM_SQDIFF
bottom_right = (top_left[0] + width, top_left[1] + height)
cv2.rectangle(img_rgb, top_left, bottom_right, (255, 0, 0), 2) 

cv2.imshow("Matched image", img_rgb)
cv2.waitKey()
cv2.destroyAllWindows()

标签: python-3.xopencv

解决方案


推荐阅读