1

I am trying to use an adaptive threshold on a 5 MB image whose Pixel is around 4017 x 3007

While using the simple code of threshold as mentioned below:

import cv2
import numpy as np

img = cv2.imread('p2.png')
#retval, threshold = cv2.threshold(img, pixel parameter below (will be black), pixel parameter above (will be white), cv2.THRESH_BINARY)
retval, threshold = cv2.threshold(img, 140 , 255, cv2.THRESH_BINARY)

cv2.imshow('threshold', threshold)

#For gray Scale
grayscaled = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 120 , 255, cv2.THRESH_BINARY)
cv2.imshow('threshold2', threshold2)

#Gray Scale with Gaussian for Adaptive threshold to give a clear Image
gauss = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 115,1)
cv2.imshow('gauss', gauss)

#otsu Threshold
retval2, otsu = cv2.threshold(grayscaled, 150,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('otsu', otsu)


cv2.imshow('original', threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

the image displayed by OpenCV is not correct and it displayed only the top left side of an image not the whole image

But the same thing while using with matploatlib using below code:

import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = cv2.imread("p2.JPG")
ret,threshold = cv2.threshold(image,127,255,cv2.THRESH_BINARY)
th = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
plt.imshow(threshold)
plt.axis("off")
#plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
plt.show()

I am able to set the threshold also but when it comes to an adaptive threshold to use with the image then the error comes up like this:

    th = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1627: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'

Any suggestion on this will be very helpful

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
varul jain
  • 334
  • 8
  • 23
  • I am following this link: (https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html) and trying to only display the adaptive threshold but while doing this I am still finding the same issue the problem I guess is due to Global Thresholding which I am not able to define. – varul jain Apr 30 '19 at 06:48
  • Possible dupe of https://stackoverflow.com/questions/35180764/opencv-python-image-too-big-to-display – Patrick Artner Apr 30 '19 at 07:15

1 Answers1

1

Your image is too big to be shown fully, only part of it is shown, you need to downscale your output windows:

# prepare windows, scale to 600x600px:
for n in ["threshold","threshold2", "gauss", "otsu","original"]:
    cv2.namedWindow(n,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(n, 600,600)

before you show them.

You used a wrong image for the 'original' titles window - I fixed that as well:

import cv2     
import numpy as np

# changed p2.png
img = cv2.imread('./big.png')  # big.png: 5000*5000 image - change it to your name again!

# prepare windows, scale to 600x600px:
for n in ["threshold","threshold2", "gauss", "otsu","original"]:
    cv2.namedWindow(n,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(n, 600,600)

img = cv2.imread('p2.png')
retval, threshold = cv2.threshold(img, 140 , 255, cv2.THRESH_BINARY) 
cv2.imshow('threshold', threshold)

#For gray Scale
grayscaled = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 120 , 255, cv2.THRESH_BINARY)
cv2.imshow('threshold2', threshold2)

#Gray Scale with Gaussian for Adaptive threshold to give a clear Image
gauss = cv2.adaptiveThreshold(grayscaled, 255, 
                              cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 115,1)
cv2.imshow('gauss', gauss)

#otsu Threshold
retval2, otsu = cv2.threshold(grayscaled, 150,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('otsu', otsu)


cv2.imshow('original', img)   # fixed here to show the original
cv2.waitKey(0)
cv2.destroyAllWindows()

Your 2nd code block feeds a wrong image format into the function, hence the assertion exception:

error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'

Your input to it has to conform to be CV_8UC1 ... did you check if you provide the correct inputs? You need to input a cv2.cvtColor(image , cv2.COLOR_BGR2GRAY) version of your image.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69