import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('5.jpg')
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
blue_min = np.array([100, 100, 100], np.uint8)
blue_max = np.array([140, 255, 255], np.uint8)
mask_inverse = cv2.inRange(img_hsv, blue_min, blue_max)
mask = cv2.bitwise_not(mask_inverse)
plt.imshow(cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB))
mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
masked_upstate = cv2.bitwise_and(img, mask_rgb)
masked_replace_white = cv2.addWeighted(masked_upstate, 1, cv2.cvtColor(mask_inverse, cv2.COLOR_GRAY2RGB), 1, 0)
plt.imshow(cv2.cvtColor(masked_replace_white, cv2.COLOR_BGR2RGB))
plt.show()
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('5.jpg')
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
blue_min = np.array([100, 100, 100], np.uint8)
blue_max = np.array([140, 255, 255], np.uint8)
mask_inverse = cv2.inRange(img_hsv, blue_min, blue_max)
mask = cv2.bitwise_not(mask_inverse)
plt.imshow(cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB))
mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
masked_upstate = cv2.bitwise_and(img, mask_rgb)
masked_replace_white = cv2.addWeighted(masked_upstate, 1, cv2.cvtColor(mask_inverse, cv2.COLOR_GRAY2RGB), 1, 0)
plt.imshow(cv2.cvtColor(masked_replace_white, cv2.COLOR_BGR2RGB))
plt.show()
This media is not supported in your browser
VIEW IN TELEGRAM
سه روش آستانه گیری:
+ Simple thresholding
+ Adaptive thresholding
+ Otsu's thresholding
+ Simple thresholding
+ Adaptive thresholding
+ Otsu's thresholding
Simple Thresholding
اگر مقدار پیکسل از مقدار آستانه بزرگتر باشد،یک مقدار میگیره (ممکنه اون سفید باشه) و اگر کمتر باشه (ممکنه اون مقدار سیاه باشه).
در آستانه گیری ساده ا ز تابع cv2.threshold استفاده میکنیم.
- اولین آرگومان این تابع مربوط به تصویر اولیه میباشد که باید سیاه و سفید باشد.
- آرگومان دوم مقدار آستانه ای که برای طبقه بندی مقادیر پیکسل استفاده میشود.
- آرگومان سوم maxVal که نشان دهنده
بیشترین مقدار از مقدار آستانه است.
- سبک های مختلف آستانه توسط آرگومان چهارم مشخص میشود:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
اگر مقدار پیکسل از مقدار آستانه بزرگتر باشد،یک مقدار میگیره (ممکنه اون سفید باشه) و اگر کمتر باشه (ممکنه اون مقدار سیاه باشه).
در آستانه گیری ساده ا ز تابع cv2.threshold استفاده میکنیم.
- اولین آرگومان این تابع مربوط به تصویر اولیه میباشد که باید سیاه و سفید باشد.
- آرگومان دوم مقدار آستانه ای که برای طبقه بندی مقادیر پیکسل استفاده میشود.
- آرگومان سوم maxVal که نشان دهنده
بیشترین مقدار از مقدار آستانه است.
- سبک های مختلف آستانه توسط آرگومان چهارم مشخص میشود:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
import cv2
import numpy as np
img = cv2.imread('sample.jpg',0)
retval,threshold = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imshow('Original ',img)
cv2.imshow('threshold ',threshold)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import numpy as np
img = cv2.imread('sample.jpg',0)
retval,threshold = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imshow('Original ',img)
cv2.imshow('threshold ',threshold)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import cv2
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import cv2
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import cv2
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import cv2
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
import numpy as np
img = cv2.imread('sample.jpg',0)
ret,threshold1 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
cv2.imshow('threshold ',threshold1)
cv2.waitKey (0)
cv2.destroyAllWindows ()
This media is not supported in your browser
VIEW IN TELEGRAM
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('8.jpg',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('8.jpg',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()