Jas*_*son 7 python numpy image-processing
我正在编写一个脚本来修改RGB图像的亮度,使用NumPy和CV2通过从RGB转换为YCrCb再返回.但是,我正在使用的循环需要一段时间才能执行,我想知道是否有更快的方法.
import cv2 as cv, numpy as np
threshold = 64
image = cv.imread("motorist.jpg", -1)
image.shape # Evaluates to (1000, 1500, 3)
im = cv.cvtColor(image, cv.COLOR_RGB2YCR_CB)
for row in image:
for col in row:
if col[0] > threshold:
col[0] = threshold
image = cv.cvtColor(im, cv.COLOR_YCR_CB2RGB)
cv.imwrite("motorist_filtered.jpg", image)
Run Code Online (Sandbox Code Playgroud)
实现阈值比较的嵌套循环至少需要5-7秒才能执行.有没有更快的方法来实现此功能?
Hoo*_*ked 16
我们的想法是创建一个掩码,让你使用numpy的矢量化.由于形状是(n,m,3),在前两个维度上循环并抓住最后一个维度的第一个索引[:,:,0]
idx = image[:,:,0] > threshold
image[idx,0] = threshold
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20536 次 |
| 最近记录: |