An Example of python opencv Binary Calculation of White Pixels

  • 2021-07-09 08:51:43
  • OfStack

Paste part of the code


#! /usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import Image

area = 0
def ostu(img):
  global area
  image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #  Turn to gray 
  blur = cv2.GaussianBlur(image,(5,5),0) #  Threshold 1 Be sure to set to  0  ! Gaussian ambiguity 
  ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # 2 Valuation  0 = black ; 1 = white
  # cv2.imshow('image', th3)
  # a = cv2.waitKey(0)
  # print a
  height, width = th3.shape
  for i in range(height):
    for j in range(width):
      if th3[i, j] == 255:
        area += 1
  return area

Related articles: