python_developer
1.54K subscribers
3.74K photos
1.97K videos
232 files
434 links
کانال اموزش پایتون @Real_developer
تبلیغات پذیرفته میشود.
Download Telegram
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('bimodal_hsv_noise.png',0)

# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]

for i in xrange(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()
σ2w(t)=q1(t)σ21(t)+q2(t)σ22(t)
where

q1(t)=∑i=1tP(i)&q1(t)=∑i=t+1IP(i)
μ1(t)=∑i=1tiP(i)q1(t)&μ2(t)=∑i=t+1IiP(i)q2(t)
σ21(t)=∑i=1t[i−μ1(t)]2P(i)q1(t)&σ22(t)=∑i=t+1I[i−μ1(t)]2P(i)q2(t)
python_developer
σ2w(t)=q1(t)σ21(t)+q2(t)σ22(t) where q1(t)=∑i=1tP(i)&q1(t)=∑i=t+1IP(i) μ1(t)=∑i=1tiP(i)q1(t)&μ2(t)=∑i=t+1IiP(i)q2(t) σ21(t)=∑i=1t[i−μ1(t)]2P(i)q1(t)&σ22(t)=∑i=t+1I[i−μ1(t)]2P(i)q2(t)
این تصویر یک bimodal_hsv نویز دار است. برای همین ما از تابع cv2.threshold() استفاده میکنیم ولی یک فلگ اضافی ، cv2.THRESH_OTSU را داریم . این روش مقدار بهینه آستانه را در retVal قرار میدهد و مقدار threshold برابر 0 میشود. همچنین با فیلترینگ نویز را برطرف شده است .
This media is not supported in your browser
VIEW IN TELEGRAM
در #قطعه_بندی تقسیم تصاوبر به نواحی غیر یکسان میباشد، طوری که با هم اشتراکی نداشته باشند.
python_developer
σ2w(t)=q1(t)σ21(t)+q2(t)σ22(t) where q1(t)=∑i=1tP(i)&q1(t)=∑i=t+1IP(i) μ1(t)=∑i=1tiP(i)q1(t)&μ2(t)=∑i=t+1IiP(i)q2(t) σ21(t)=∑i=1t[i−μ1(t)]2P(i)q1(t)&σ22(t)=∑i=t+1I[i−μ1(t)]2P(i)q2(t)
آستانه گذاری #دوسطحی یک مقدار آستانه دارد و پیکسلهای تصویر به دو گروه یا کلاس تقسیم میشوند. انتخاب آستانه در این گروه تصاویر بسیار ساده از آستانه گذاری چند سطحی میباشد.
💢💢💢💢💢💢💢💢💢💢
اگر تصوير به خوبي دو سطحي نشود ، اطلاعات زيادي از بين خواهد رفت.
یعنی تصویر خاکستری(Scale-Gray)
(تصویر 8 بیتی یا 1 بایتی ، هر پپکسل مقداری بین 0-255 میگیرد. )به دو دویی( تصویر 1بیتی ، در هر پیکسل 1یا0 ذخیره میشود. در واقع همان سفید مطلق و سیاه مطلق ) تبدیل شود.
تک آستانه، دو سطحی
اینجا پیکسلهای سفید پس زمینه و سیاه، شی یا پیش زمینه را نشان میدهند.
تصویر که دارای بیش از یک آستانه برای آستانه گذاری چند سطحی میباشد.
آستانه گیری چند سطحی با دو آستانه
python_developer
آستانه گیری چند سطحی با دو آستانه
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('photo_2017-10-07_16-19-40.jpg',0)
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in xrange(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show
python_developer
روش Simple Thresholding در عکسهای رنگی جالب نیست و خروجی آن شبیه تصویر میباشد .
عکس هایی که دارای روشنایی متفاوت در قسمتهای مختلف میباشند خروجی جالبی ندارند.(به اشتباه رنگی نوشتم)
This media is not supported in your browser
VIEW IN TELEGRAM