Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import sys
- import os.path
- import trans #pip install trans
- import time
- import cv2
- import math
- import skimage
- import numpy as np
- from skimage.morphology import skeletonize
- from skimage import util
- ap = argparse.ArgumentParser()
- ap.add_argument("-i", "--image", required = True,
- help = "Path to the image")
- args = vars(ap.parse_args())
- img = cv2.imread(args["image"])
- # create gray image for further processing
- #gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- #cv2.imwrite("gray.png", gray_img)
- # create a CLAHE object (Arguments are optional).
- clahe = cv2.createCLAHE(clipLimit=3.25, tileGridSize=(4,4))
- # apply CLAHE histogram expansion to find squares better with canny edge detection
- #gray_img = clahe.apply(gray_img)
- #cv2.imwrite("CLAHE.png", gray_img)
- #gray_blur = cv2.GaussianBlur(gray_img, (15, 15), 0)
- #thresh = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 15, 1)
- #cv2.imwrite("adaptthresh.png", thresh)
- #kernel = np.ones((3, 3), np.uint8)
- #closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=4)
- #cv2.imwrite("close.png", closing)
- #pyrMSF = cv2.pyrMeanShiftFiltering(img, 30, 10)
- #cv2.imwrite("pyrMSF.png", pyrMSF)
- #edges = cv2.Canny(img, 100, 200)
- #cv2.imwrite("coloredges.png", edges)
- #edges = cv2.Canny(gray_img, 100, 200)
- #cv2.imwrite("grayedges.png", edges)
- #-----Converting image to LAB Color model-----------------------------------
- lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
- #-----Splitting the LAB image to different channels-------------------------
- l, a, b = cv2.split(lab)
- #-----Applying CLAHE to L-channel-------------------------------------------
- cl = clahe.apply(l)
- #-----Merge the CLAHE enhanced L-channel with the a and b channel-----------
- limg = cv2.merge((cl,a,b))
- #-----Converting image from LAB Color model to RGB model--------------------
- final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
- cv2.imwrite("1colorCLAHE.png", final)
- #_____END_____#
- blur = cv2.GaussianBlur(final,(5,5),0)
- cv2.imwrite("2colorCLAHEgauss.png", blur)
- meanshift = cv2.pyrMeanShiftFiltering(blur, sp=75, sr=30, maxLevel=1, termcrit=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 5, 1))
- cv2.imwrite("3meanshiftCLAHE.png", meanshift)
- edgeCLAHE = cv2.Canny(meanshift, 100, 200)
- cv2.imwrite("4edgeCLAHE.png", edgeCLAHE)
- #colCLAHEpyrMSF = cv2.pyrMeanShiftFiltering(final, 30, 10)
- #cv2.imwrite("colorCLAHEpyrMSF.png", colCLAHEpyrMSF)
- #img_float = np.float32(final) # Convert image from unsigned 8 bit to 32 bit float
- #criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 10, 1)
- # Defining the criteria ( type, max_iter, epsilon )
- # cv2.TERM_CRITERIA_EPS - stop the algorithm iteration if specified accuracy, epsilon, is reached.
- # cv2.TERM_CRITERIA_MAX_ITER - stop the algorithm after the specified number of iterations, max_iter.
- # cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER - stop the iteration when any of the above condition is met.
- # max_iter - An integer specifying maximum number of iterations.In this case it is 10
- # epsilon - Required accuracy.In this case it is 1
- #k = 15 # Number of clusters
- #ret, label, centers = cv2.kmeans(img_float, k, None, criteria, 50, cv2.KMEANS_RANDOM_CENTERS)
- # apply kmeans algorithm with random centers approach
- #center = np.uint8(centers)
- # Convert the image from float to unsigned integer
- #res = center[label.flatten()]
- # This will flatten the label
- # res2 = res.reshape(img.shape)
- # # Reshape the image
- # cv2.imwrite("1.jpg", res2) # Write image onto disk
- # #meanshift = cv2.pyrMeanShiftFiltering(img, sp=8, sr=16, maxLevel=1, termcrit=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 5, 1))
- # #cv2.imwrite("2.jpg", meanshift)
- #
- #
- # # Write image onto disk
- # gray = cv2.cvtColor(final, cv2.COLOR_BGR2GRAY)
- # # Convert image from RGB to GRAY
- # ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
- # # apply thresholding to convert the image to binary
- # fg = cv2.erode(thresh, None, iterations=1)
- # # erode the image
- # bgt = cv2.dilate(thresh, None, iterations=1)
- # # Dilate the image
- # ret, bg = cv2.threshold(bgt, 1, 128, 1)
- # # Apply thresholding
- # marker = cv2.add(fg, bg)
- # # Add foreground and background
- # canny = cv2.Canny(marker, 110, 150)
- # # Apply canny edge detector
- # new, contours, hierarchy = cv2.findContours(canny, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- # # Finding the contors in the image using chain approximation
- # marker32 = np.int32(marker)
- # # converting the marker to float 32 bit
- # cv2.watershed(final, marker32)
- # # Apply watershed algorithm
- # m = cv2.convertScaleAbs(marker32)
- # ret, thresh = cv2.threshold(m, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
- # # Apply thresholding on the image to convert to binary image
- # thresh_inv = cv2.bitwise_not(thresh)
- # # Invert the thresh
- # res = cv2.bitwise_and(final, final, mask=thresh)
- # # Bitwise and with the image mask thresh
- # res3 = cv2.bitwise_and(final, final, mask=thresh_inv)
- # # Bitwise and the image with mask as threshold invert
- # res4 = cv2.addWeighted(res, 1, res3, 1, 0)
- # # Take the weighted average
- # final = cv2.drawContours(res4, contours, -1, (0, 255, 0), 1)
- # # Draw the contours on the image with green color and pixel width is 1
- # cv2.imwrite("3.jpg", final) # Write the image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement