简体   繁体   English

如何使用 tkinter 在 tiff 图像上选择矩形区域并提取矩形内的像素值?

[英]How to select rectangle region on tiff image using tkinter and extract pixel values within the rectangle?

I want to select a rectangle region over an image using mouse events and extract the values within the rectangle as a list or an array or a tuple.我想使用鼠标事件在图像上选择一个矩形区域,并将矩形内的值提取为列表、数组或元组。 I have found no way of how to achieve this.我没有找到如何实现这一目标的方法。

Does anyone know any way of achieving this or any tips or suggestions?有谁知道实现这一目标的任何方法或任何提示或建议?

Thank you so much for reading.非常感谢您的阅读。

The function to use is cv.selectROI要使用的函数是cv.selectROI

It can take different arguments.它可以采用不同的参数。 The simplest case is just taking an image (numpy array).最简单的情况就是拍摄图像(numpy 数组)。 Here's an example where also the window name is specified.这是一个示例,其中还指定了窗口名称。

Personally I also prefer to use the fromCenter argument.我个人也更喜欢使用fromCenter参数。

import numpy as np
import cv2 as cv

im = cv.imread(cv.samples.findFile("lena.jpg"))

bbox = cv.selectROI("lena", im, fromCenter=True)
cv.destroyWindow("lena")

print("region:", bbox)

(x,y,w,h) = bbox

subregion = im[y:y+h, x:x+w]
cv.imshow("lena subregion", subregion)
cv.waitKey(-1)
cv.destroyWindow("lena subregion")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM