<p>我正在Ubuntu上开发python-Open-CV。
我对python相当陌生,我觉得我的代码没有得到优化。在</p>
<p>最终目标是将像素颜色更改为jpeg图像。假设红色通道值为<;255,我将其设置为255。在</p>
<p>为此,我将jpeg转换为数字阵列一。
然后使用“for/in:”循环逐像素检查红色通道是否为<;255。如果满足条件,则将值更改为255。在</p>
<p>我的代码:</p>
<pre><code>import numpy
import cv2
img=cv2.imread('image.jpeg',1)
y=x=-1 # I use y and x as a counters.
#They will track the pixel position as (y,x)
for pos_y in img:
y=y+1; x=-1 #For each new column of the image x is reset to -1
for pos_x in pos_y:
x=x+1
b, g, r = pos_x # I get the blue, green and red color
# please note that opencv is bgr instead of rgb
if r < 255:
r = 255
pos_x = [b,g,r]
img[y,x] = pos_x
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>
<p>这个代码有效。但是,我觉得既不优雅也不高效。在</p>
<p>如何优化代码并使其更高效?在</p>