OpenCV标注工具加载图像不全问题求助
问题:OpenCV标注工具加载图像显示不全
一、使用的屏幕截图代码
我用以下Python代码实现屏幕截图:
import numpy as np import win32gui, win32ui, win32con class WindowCapture: # properties w = 0 h = 0 hwnd = None cropped_x = 0 cropped_y = 0 offset_x = 0 offset_y = 0 # constructor def __init__(self, window_name): # find the handle for the window we want to capture self.hwnd = win32gui.FindWindow(None, window_name) if not self.hwnd: raise Exception('Window not found: {}'.format(window_name)) # get the window size window_rect = win32gui.GetWindowRect(self.hwnd) self.w = window_rect[2] - window_rect[0] self.h = window_rect[3] - window_rect[1] # account for the window border and titlebar and cut them off border_pixels = 10 titlebar_pixels = 30 self.w = self.w - (border_pixels * 2) self.h = self.h - titlebar_pixels - border_pixels self.cropped_x = border_pixels self.cropped_y = titlebar_pixels # set the cropped coordinates offset so we can translate screenshot # images into actual screen positions self.offset_x = window_rect[0] + self.cropped_x self.offset_y = window_rect[1] + self.cropped_y def get_screenshot(self): # get the window image data wDC = win32gui.GetWindowDC(self.hwnd) dcObj = win32ui.CreateDCFromHandle(wDC) cDC = dcObj.CreateCompatibleDC() dataBitMap = win32ui.CreateBitmap() dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h) cDC.SelectObject(dataBitMap) cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY) # convert the raw data into a format opencv can read dataBitMap.SaveBitmapFile(cDC, 'debug.bmp') signedIntsArray = dataBitMap.GetBitmapBits(True) img = np.fromstring(signedIntsArray, dtype='uint8') img.shape = (self.h, self.w, 4) # free resources dcObj.DeleteDC() cDC.DeleteDC() win32gui.ReleaseDC(self.hwnd, wDC) win32gui.DeleteObject(dataBitMap.GetHandle()) # drop the alpha channel, or cv.matchTemplate() will throw an error like: # error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() # && _img.dims() <= 2 in function 'cv::matchTemplate' img = img[...,:3] # make image C_CONTIGUOUS to avoid errors that look like: # File ... in draw_rectangles # TypeError: an integer is required (got type tuple) # see the discussion here: # https://github.com/opencv/opencv/issues/14866#issuecomment-580207109 img = np.ascontiguousarray(img) return img # find the name of the window you're interested in. # once you have it, update window_capture() # https://stackoverflow.com/questions/55547940/how-to-get-a-list-of-the-name-of-every-open-window @staticmethod def list_window_names(): def winEnumHandler(hwnd, ctx): if win32gui.IsWindowVisible(hwnd): print(hex(hwnd), win32gui.GetWindowText(hwnd)) win32gui.EnumWindows(winEnumHandler, None) # translate a pixel position on a screenshot image to a pixel position on the screen. # pos = (x, y) # WARNING: if you move the window being captured after execution is started, this will # return incorrect coordinates, because the window position is only calculated in # the __init__ constructor. def get_screen_position(self, pos): return (pos[0] + self.offset_x, pos[1] + self.offset_y)
最初截图遇到问题,在主函数文件中导入pyautogui后解决。
二、标注工具图像显示问题
现在我在训练级联分类器识别物品特征,使用OpenCV官方标注工具时,加载目标图像后显示不全:
- 原始图像:

- 工具显示结果:

我参考相关教程并对代码做了少量调整,现在需要解决这个图像显示不全的问题。
三、可行的解决方法
- 缩放图像分辨率:旧版OpenCV标注工具对超大分辨率图像支持有限,先把图像缩放到1920x1080以内的尺寸,再导入工具
- 调整工具窗口:最大化标注工具窗口,或者拖动窗口边缘放大,查看是否能显示完整图像
- 更换工具版本:改用OpenCV 4.x系列的标注工具,新版对图像显示的兼容性更好
- 转换图像格式:将JPG格式转成PNG后再尝试加载,避免格式解码异常
- 检查图像完整性:用本地图像查看器确认原始图像没有损坏,重新导出或获取图像
内容的提问来源于stack exchange,提问作者khalid bushawareb
相关产品推荐
相关产品推荐

