Python Briefly Expresses Long If Statements

Asked 2 years ago, Updated 2 years ago, 72 views

import numpy as np
import cv2 as cv

img = cv.imread('dp.png')

img_h = img.shape[0]
img_w = img.shape[1]
img_bpp = img.shape[2]

print(img_h, img_w, img_bpp)

blue = (255,0,0)
white = (255,255,255)

a = 450
b = 0
c = list()
d = list()
e = 0

bthick = 12


 for b in range(0,img_h-12):
   if (img[b,a] == [255,255,255]).all() and \
(img[b+1,a] != [255,255,255]).all() and \
(img[b+2,a] != [255,255,255]).all() and \
(img[b+3,a] != [255,255,255]).all() and \
(img[b+4,a] != [255,255,255]).all() and \
(img[b+5,a] != [255,255,255]).all() and \
(img[b+6,a] != [255,255,255]).all() and \
(img[b+7,a] != [255,255,255]).all() and \
(img[b+8,a] != [255,255,255]).all() and \
(img[b+9,a] != [255,255,255]).all() and \
(img[b+10,a] != [255,255,255]).all() and \
(img[b+11,a] != [255,255,255]).all() and \
(img[b+12,a] != [255,255,255]).all() :
    b = b + 1
    d.append(b)

  else :
    b = b + 1

print('d=')
print(d)

Here's my code.

In coordinates (a, b), a=450 (fixed) and b is +1 from 0 to (img_h-12) (image end).

A point where pixels in (a,b) coordinates are white and pixels from (a, b+1) to (a, b+12) are not white.

Code for putting b values (y coordinates) into the d array.

This condition is not a code that is entered 12 times directly as shown above

I have to express it using bthick = 12, but I've been getting errors for two days.

How should I express this? ㅠ<

python for if문

2022-09-22 11:09

2 Answers

Hello. :-)

I'm not a Python expert, but I've written around 'working code'. Please just refer to it briefly. (Based on 2.7.x)

import numpy as np
import cv2 as cv

img = cv.imread('dp.png')

img_h = img.shape[0]
img_w = img.shape[1]
img_bpp = img.shape[2]

print(img_h, img_w, img_bpp)

blue = (255,0,0)
white = (255,255,255)

a = 450
b = 0
c = list()
d = list()
e = 0

bthick = 12

#--------------------------------------------------
# Add Verification Function: check_func
#--------------------------------------------------
def check_func(i):
  trd_hold = [255,255,255] #RGBThreshold
  If i==0: # The first must be true
    return (img[b+i,a] == trd_hold).all()
  Else: Other than that, it has to be different to be true
    return (img[b+i,a] != trd_hold).all()


for b in range(0,img_h-12):
# The map function extracts an integer from 0 to 12 by the range function and passes it to the argument of check_func to call it.
  if all( map(check_func, range(0,bthick+1)) ):
    b = b + 1
    d.append(b)
  else :
    b = b + 1

print('d=')
print(d)

The key is to use the map function, and it is a very useful child, so it would be good to search for it yourself.

Express the style of shortening long codes with Python simplicity as Pythonic. It's not my code, of course.Haha

I hope it was helpful.

Thank you.


2022-09-22 11:09

You want to calculate with pixels on the Y-axis, right?

The problem is that if you read it as an imread, it's (Y, X), so that img[0] is the horizontal x-axis pixel values (arrays) that would make the same operation as the question.

So why don't we get the transposition matrix and spin it 90 degrees?

If we obtain the transposition matrix as shown below, img[0] is the array of the pixel values on the Y-axis.

The array comparison can also be done as shown in Example.

import cv2

img = cv2.imread('lines-300x209.png')
img.shape
Out[4]: (209, 300, 3)

timg = cv2.transpose(img)
timg.shape
Out[5]: (300, 209, 3)

timg[30][0:11]
Out[6]: array([[  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [103, 103, 103]], dtype=uint8)

(timg[30][0:11] == (((0, 0, 0),) * 11)).All() #array 11 comparisons. The last one is [103, 103, 103], so it's false 
Out[7]: False

(timg[30][0:10] == (((0, 0, 0),) * 10)).All() #array 10 comparisons Since all 10 are the same True
Out[8]: True

(timg[30][0:10] == (((0, 0, 0),) * 10)).all() and (timg[30][10] == (103, 103, 103)).True because all() # and 11th element comparison is the same
Out[9]: True

Oh also transposed matrix, instead of seeking 30 column, as shown below can be obtained.

img[:,30] #Column 30 array.

img[:,30][0:11] # Same as the transpose above.
Out[15]: array([[  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [  0,   0,   0],
       [103, 103, 103]], dtype=uint8)


2022-09-22 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.