1

I have 2 types of image to deal. one with white background and another type with dark background. My requirement is to apply different thresholds for each type

for ex : for white back ground

(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

for dark back ground

(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY_INV  + cv2.THRESH_OTSU)

I am reading images using cv.imread(img,0)

I am doing morphological transformation , so i need to invert the white back ground image. but for the dark background i don't want to invert.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    To determine if the background is a majority white or black, you can use `cv2.countNonZero()`. One approach is if the returned value (white pixels) is greater than 50% of the entire image then the background is white, else the background is black. Depending on the result, you can then apply the appropriate threshold – nathancy Sep 09 '19 at 20:02
  • Cool - please show some images and say what your question is or which aspect you are getting stuck with. – Mark Setchell Sep 09 '19 at 20:48
  • This worked for me very well .cv2.countNonZero() Thank you – Suresh Anand Sep 12 '19 at 16:07

1 Answers1

0

To expand on @nathancy's comment, you could use one of the OpenCV functions, sum or mean

For an plain old 24 bit color image, black and white are represented by ( 0, 0, 0) black (255, 255, 255) white

Here is a sample image that looks like a page out of a book: enter image description here

Now let's run some code on it

import cv2 as cv
import numpy as np

img = cv.imread('lorem_ipsum.png',cv.IMREAD_COLOR)

ret = cv.mean(img)
print(ret)

ret = cv.mean(ret)
print(ret)

ret = ret*4/3
print(ret)

ret = cv.mean(cv.mean(img))[0]*4/3
print(ret)

which gives output:

(229.78, 228.28, 228.95, 0.0)
(171.74, 0.0, 0.0, 0.0)
228.98
228.98

The first line gives us the mean of blue, green, red, and alpha channels. Second line is mean of the means. Because the first line had a zero entry, the mean of means is too low. We want to ignore alpha channel. So on the last line we pick off just the first element in mean of means and scale it by 4/3 to get a 0 to 255 answer. Our answer is 228.98 --> the picture is mostly white. The last line is the result of doing all of the operations in one line.

bfris
  • 5,272
  • 1
  • 20
  • 37