OpenCV Python:标准化图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40645985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:46:06  来源:igfitidea点击:

OpenCV Python: Normalize image

pythonopencv

提问by kot09

I'm new to OpenCV. I want to do some preprocessing related to normalization. I want to normalize my image to a certain size. The result of the following code gives me a black image. Can someone point me to what exactly am I doing wrong? The image I am inputting is a black/white image

我是 OpenCV 的新手。我想做一些与规范化相关的预处理。我想将我的图像规范化为特定大小。以下代码的结果给了我一个黑色图像。有人可以指出我到底做错了什么吗?我输入的图像是黑白图像

import cv2 as cv
import numpy as np

img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
cv.normalize(img,  normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', self.normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()

回答by Ophir Carmi

as one can see at: http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#cv2.normalize, there is a → dstthat say that the result of the normalizefunction is returned as output parameter. The function doesn't change the input parameter dstin-place. (The self.in cv.imshow('dst_rt', self.normalizedImg)line is a typo)

正如在:http: //docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#cv2.normalize所见,有一种→ dst说法是normalize函数的结果作为输出参数返回。该函数不会dst就地更改输入参数。(该self.cv.imshow('dst_rt', self.normalizedImg)线是一个错字)

import cv2 as cv
import numpy as np
path = r"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"
img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
normalizedImg = cv.normalize(img,  normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()

回答by Jo?o Cartucho

It's giving you a black image because you are probably using different sizes in img and normalizedImg.

它给你一个黑色图像,因为你可能在 img 和 normalizedImg 中使用了不同的大小。

import cv2 as cv

img = cv.imread(path)
img = cv.resize(img, (800, 800))
cv.normalize(img, img, 0, 255, cv.NORM_MINMAX)

cv.imshow('dst_rt', img)
cv.waitKey(0)
cv.destroyAllWindows()

回答by rocklegend

When you call cv.imshow()you use self.normalizedImg, instead of normalizedImg.

当你打电话时,cv.imshow()你使用self.normalizedImg, 而不是normalizedImg

The self. is used to identify class members and its use in the code you've written is not appropriate. It shouldn't even run as written. However I assume this code has been extracted from a class definition, but you must be consistent in naming variables and self.normalizedImgis different from normalizedImg.

自己。用于标识类成员,在您编写的代码中使用它是不合适的。它甚至不应该像写的那样运行。但是我假设这段代码是从一个类定义中提取出来的,但是你必须在命名变量上保持一致并且self.normalizedImg不同于normalizedImg.