You are on page 1of 14

4/30/2021 temp-161977272719037728

In [101]:
# Buat daftar untuk menyimpan url gambar
urls = ["https://i.pinimg.com/564x/c4/66/65/c46665e47cf56642a598f1a0040bae8f.jpg"
]

# Fungsi untuk Baca dan tampilkan gambar


def cv_imshow(img):
plt.figure(figsize= (15,15))
plt.imshow(img)
plt.xticks([]), plt.yticks
([])
plt.show()

# loop terhadap URL gambar, Anda dapat menyimpan beberapa url gambar dalam daftar

for url in urls:


image= io.imread (url)
image_2= cv.cvtColor (image, cv.COLOR_BGR2RGB
)
final_frame = cv.hconcat((image, image_2
))
cv_imshow(final_frame)

In [102]:

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 1/14
4/30/2021 temp-161977272719037728

plt.hist(image.ravel(),bins = 256, range = [0,256])


plt.show()

In [103]:
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv.calcHist([image],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()

In [104]:
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv_imshow(gray_image)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 2/14
4/30/2021 temp-161977272719037728

In [105]:

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 3/14
4/30/2021 temp-161977272719037728

# Plot histogram gambar abu-abu.


# Kita bisa mengamati bahwa frekuensi histori citra mengalami penurunan ~ 1/3 dari hist
ogram citra berwarna
plt.hist
(gray_image
.ravel
(),bins= 256, range= [0, 256])
plt.show
()

In [106]:
# myGrayImg = cv.cvtColor(myImg, cv.COLOR_BGR2GRAY)
# cv_imshow()

In [107]:
plt.contour(gray_image, origin = "image")

Out[108]:

<matplotlib.contour.QuadContourSet at 0x7f1053d3bad0> In [109]:

# Setel ambang batas untuk deteksi kontur ret,

thresh = cv.threshold(gray_image,100,200,0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(image, contours, -1, (0, 255, 0), 3) plt.imshow(image)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 4/14
4/30/2021 temp-161977272719037728

Out[109]:

<matplotlib.image.AxesImage at 0x7f1053576050>

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 5/14
4/30/2021 temp-161977272719037728

In [108]:
im2 = 255 - gray_image
cv_imshow(im2)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 6/14
4/30/2021 temp-161977272719037728

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 7/14
4/30/2021 temp-161977272719037728

In [109]:
im3 = (100.0/255)*gray_image + 100
cv_imshow(im3)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 8/14
4/30/2021 temp-161977272719037728

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 9/14
4/30/2021 temp-161977272719037728

In [112]:
im4 = 255.0*(gray_image/255.0)**2
cv_imshow(im4)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 10/14
4/30/2021 temp-161977272719037728

In [113]:

def histeq(im, nbr_bins = 256):


""" Persamaan histogram dari citra grayscale. """
# dapatkan histogram gambar
imhist, bins = np.histogram(im.flatten(), nbr_bins, [0, 256])
cdf = imhist.cumsum() # fungsi distribusi kumulatif cdf =
imhist.max()*cdf/cdf.max() #normalisasi cdf_mask =
np.ma.masked_equal(cdf, 0)
cdf_mask = (cdf_mask - cdf_mask.min())*255/(cdf_mask.max()-
cdf_mask.min()) cdf = np.ma.filled(cdf_mask,0).astype('uint8') return
cdf[im.astype('uint8')]

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 11/14
4/30/2021 temp-161977272719037728

In [114]:
im5 = histeq(im4)
cv_imshow(im5)

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 12/14
4/30/2021 temp-161977272719037728

In [115]:

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 13/14
4/30/2021 temp-161977272719037728

plt.hist(im4.ravel(),bins = 256, range = [0, 256])


plt.show()

https://htmtopdf.herokuapp.com/ipynbviewer/temp/ae2c470261d80ebb8b9038df8b6b30c3/Untitled3.html?t=1619772733026 14/14

You might also like