
rt,我在使用模板匹配时将roi的值省略,而且模板图片截取也是完全没有压缩,直接保存为pgm格式,路径名也检查过了完全没有问题,但是依然显示ValueError: Expected an uncompressed image错误,请问为什么
在线等,挺急的::loudly_crying_face:
dddddddd
1,发代码,需要报错提示与全部代码文本,请注意不要贴代码图片
2,发电脑上文件管理器的截图。
你好大佬,我的问题是这样的
我想问下为什么这里总是显示没有未压缩的图片,但是我截取的图片采用的就是openmv官网给的代码,openmv进行模板匹配的图片必须是pgm格式,但是模板截取不能直接生成pgm格式,要转换格式必须进行压缩,这不是自相矛盾吗

import sensor, image
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
# 加载模板图像
template = image.Image("/image/e-5.pgm", copy_to_fb=True)
# 定义模板匹配的阈值
threshold = 0.7
while True:
# 拍摄一张图像
img = sensor.snapshot()
# 在整个图像中搜索模板
res = img.find_template(template, threshold, roi=(0, 0, img.width(), img.height()))
# 如果找到了匹配
if res:
# 获取匹配位置的中心点
cx = res[0] + res[2] // 2
# 计算图像中心点与模板匹配中心点的横坐标差距
img_center_x = img.width() // 2
distance_x = img_center_x - cx
# 打印横坐标差距到串行终端
print("Horizontal Distance:", distance_x)# Template Matching Example - Normalized Cross Correlation (NCC)
#
# This example shows off how to use the NCC feature of your OpenMV Cam to match
# image patches to parts of an image... expect for extremely controlled enviorments
# NCC is not all to useful.
#
# WARNING: NCC supports needs to be reworked! As of right now this feature needs
# a lot of work to be made into somethin useful. This script will reamin to show
# that the functionality exists, but, in its current state is inadequate.
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
#SEARCH_DS两个需要的部分,而不把image模块全部引入。
# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/image/e-5.pgm")
#加载模板图片
clock = time.clock()
# Run template matching
while (True):
clock.tick()
img = sensor.snapshot()
# find_template(template, threshold, [roi, step, search])
# ROI: The region of interest tuple (x, y, w, h).
# Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
# Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
#
# Note1: ROI has to be smaller than the image and bigger than the template.
# Note2: In diamond search, step and ROI are both ignored.
r = img.find_template(template, 0.60, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
#find_template(template, threshold, [roi, step, search]),threshold中
#的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
#注意roi的大小要比模板图片大,比frambuffer小。
#把匹配到的图像标记出来
if r:
img.draw_rectangle(r)
print(clock.fps())这是模板匹配的代码


你是怎么生成的pgm格式?你需要格式转换工具,而不是直接改后缀。