def hough_transform_image(image, img): """ :param image: pass the canny iamge :param img: pass the original image :return: it will mark the lines in the canny image and passes it back to the original image """ lines = cv2.HoughLinesP(image, 1, np.pi / 180, 50, maxLineGap=200) for line in lines: print(line) x1, x2, y1, y2 = line[0] cv2.line(img, (x1, x2), (y1, y2), (0, 255, 0), 3) show_image(img) return lines
""" Here you will find the code for low-pas-filter """ import cv2 from helpers.image_reader import image_reader, show_image def low_pass_filter(img): """ :param img: :return: """ fil = cv2.boxFilter(img, -1, (57, 57)) return fil if __name__ == "__main__": path = "/home/joemarshal/image_processing_opencv/images/shapes.jpg" mode = 1 ima = image_reader(path, mode) show_image(ima) con = cv2.cvtColor(ima, cv2.COLOR_BGR2RGB) show = low_pass_filter(con) show_image(show)
""" Here is the code to detect the shape in an imaage """ import cv2 from helpers.Shapedetector import Shapedetector from helpers.image_reader import image_reader, show_image import imutils if __name__ == "__main__": im1 = "/home/joemarshal/image_processing_opencv/images/shapes.jpg" mode = 1 img = image_reader(im1, mode) show_image(img) cv2.imwrite("/home/joemarshal/shapes.png", img) resized = imutils.resize(img, width=300) ratio = img.shape[0] / float(resized.shape[0]) gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] edges = cv2.Canny(thresh, 75, 150) show_image(edges) show_image(thresh) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = Shapedetector() for c in cnts: M = cv2.moments(c) cx = int((M["m10"] / M["m00"]) * ratio) cy = int((M["m01"] / M["m00"]) * ratio)
""" Code to compute the histogram of an image """ import cv2 from helpers.image_reader import image_reader, show_image import matplotlib.pyplot as plt if __name__ == "__main__": img = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = 0 im = image_reader(img, mode) show_image(im) cv2.imwrite("/home/joemarshal/grayronaldo.jpg", im) hist = cv2.calcHist([im], [0], None, [256], [0, 256]) print(type(hist)) show_image(hist) plt.show()
""" Here you can find how to add to images using the cv2.add() function """ import cv2 from helpers.image_reader import image_reader, show_image def add_image(image1, image2): """ :param image1: passing image1 :param image2: passing image 2 :return: adds both the images """ add = cv2.add(image1, image2) return add if __name__ == "__main__": img1 = "/home/joemarshal/image_processing/images/resizedronaldo.jpg" img2 = "/home/joemarshal/image_processing/images/resizedmessi.jpg" mode = 1 img1 = image_reader(img1, mode) img2 = image_reader(img2, mode) adding = add_image(img1, img2) show_image(adding)
""" Here you can run the threshold of an image without the threshold function """ import cv2 from helpers.image_reader import image_reader, show_image if __name__ == "__main__": img = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = cv2.IMREAD_COLOR im = image_reader(img, 0) print(im) print(im.shape) height, width = im.shape for h in range(height): for w in range(width): v = (im[h][w]) if v < 255: im[h][w] = 127 else: im[h][w] = 0 show_image(im)
from __future__ import print_function import cv2 import numpy as np from helpers.image_reader import image_reader, show_image im1 = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = 1 img = image_reader(im1, mode) for alpha in np.arange(0, 1.1, 0.1)[::-1]: if alpha > 0.0: print("transparnet image", alpha) else: print("Not Transparent", alpha) overlay = img.copy() output = img.copy() cv2.rectangle(overlay, (420, 205), (595, 385), (0, 0, 255), -1) cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output) print("alpha{}, beta{}".format(alpha, 1 - alpha)) show_image(output)
result_morphological_tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel) return result_morphological_tophat def morphological_blackhat(img): kernel = np.ones((5, 5), np.uint8) result_morphological_blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel) return result_morphological_blackhat if __name__ == "__main__": im = "/home/joemarshal/Downloads/watershed_coins_01.jpg" mode = 0 img = image_reader(im, mode) show_image(img) erotion = eroding_image(img) dilation = dilating_image(img) opening_image = opening_image_morphology(img) closing_image = closing_image_morphology(img) gradient = morphological_gradient(img) tophat = morphological_tophat(img) blackhat = morphological_blackhat(img) show_image(erotion) show_image(dilation) show_image(opening_image) show_image(closing_image) show_image(gradient) show_image(tophat) show_image(blackhat)
""" Code for Grabcut Algorithm """ import cv2 import numpy as np from helpers.image_reader import image_reader, show_image import matplotlib.pyplot as plt if __name__ == "__main__": im = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = 1 img = image_reader(im, mode) mask = np.zeros(img.shape[:2], np.uint8) bgdModel = np.zeros((1, 65), np.float64) fgdModel = np.zeros((1, 65), np.float64) rect = (50, 50, 400, 267) show_image(rect) cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') image = img * mask2[:, :, np.newaxis] plt.imshow(image), plt.colorbar(), plt.show() show_image(image) cv2.imwrite( "/home/joemarshal/image_processing_opencv/images/grabronaldo.jpg", image)
""" Here you can find the code for the brightness of the image """ import cv2 import numpy as np from helpers.image_reader import show_image if __name__ == "__main__": img = cv2.imread("/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg", 1) alpha = 2 beta = 10 result = cv2.addWeighted(img, alpha, np.zeros(img.shape, img.dtype), 0, beta) show_image(result) cv2.imwrite("/home/joemarshal/Documents/bc.jpg", result) cv2.waitKey(0) cv2.destroyAllWindows()
""" Here you will see how to add two images without the cv2.add() function """ from helpers.image_reader import image_reader, show_image def adding_images_without_add_function(imag1, imag2): """ :param imag1: passing image1 :param imag2: passing image 2 :return: adds both the images """ add1 = imag1 + imag2 return add1 if __name__ == "__main__": im1 = "/home/joemarshal/image_processing/images/resizedronaldo.jpg" im2 = "/home/joemarshal/image_processing/images/resizedmessi.jpg" mode = 1 img1 = image_reader(im1, mode) img2 = image_reader(im2, mode) add = adding_images_without_add_function(img1, img2) show_image(add)
import cv2 import matplotlib.pyplot as plt import numpy as np from helpers.image_reader import image_reader, show_image if __name__ == "__main__": im = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" img = image_reader(im, 0) show_image(img) mask = np.zeros(img.shape[:2], np.uint8) mask[82:328, 31:338] = 255 masked_img = cv2.bitwise_and(img, img, mask=mask) show_image(masked_img) hist_full = plt.hist(img.ravel(), 256, [0, 256]) hist_mask = plt.hist(masked_img.ravel(), 256, [0, 256]) plt.xlim([0, 256]) plt.show()
from skimage.feature import peak_local_max from skimage.morphology import watershed from scipy import ndimage import numpy as np import cv2 from helpers.image_reader import image_reader, show_image if __name__ == "__main__": im = "/home/joemarshal/image_processing_opencv/images/watershed_coins_01.jpg" mode = 1 img = image_reader(im, mode) show_image(img) shifted = cv2.pyrMeanShiftFiltering(img, 21, 51) show_image(shifted) gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] show_image(thresh) D = ndimage.distance_transform_edt(thresh) local_max = peak_local_max(D, indices=False, min_distance=20, labels=thresh) markers = ndimage.label(local_max, structure=np.ones((3, 3)))[0] labels = watershed(-D, markers, mask=thresh) print("[INFO] {} unique segments found".format(len(np.unique(labels)) - 1)) for label in np.unique(labels): if label == 0: continue mask = np.zeros(gray.shape, dtype="uint8")
""" Here you will see how to change the color image to black and white image using cv2.inRange() function """ import cv2 import numpy as np from helpers.image_reader import image_reader, show_image if __name__ == "__main__": img = "/home/joemarshal/image_processing/images/resizedronaldo.jpg" mode = cv2.IMREAD_COLOR # Reading the image without any change im = image_reader(img, mode) # Setting the lower threshold lower_color = np.array([[0, 60, 0]]) # setting the upper threshold upper_color = np.array([255, 150, 255]) mask = cv2.inRange(im, lower_color, upper_color) i = show_image(mask)
import cv2 import numpy as np from helpers.image_reader import image_reader, show_image def hough_transform_image(image, img): """ :param image: pass the canny iamge :param img: pass the original image :return: it will mark the lines in the canny image and passes it back to the original image """ lines = cv2.HoughLinesP(image, 1, np.pi / 180, 50, maxLineGap=200) for line in lines: print(line) x1, x2, y1, y2 = line[0] cv2.line(img, (x1, x2), (y1, y2), (0, 255, 0), 3) show_image(img) return lines if __name__ == "__main__": img1 = "/home/joemarshal/image_processing_opencv/images/lines.png" mode = 0 image1 = image_reader(img1, 1) grey = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(grey, 75, 150) show_image(edges) hough = hough_transform_image(edges, image1)
import cv2 from helpers.image_reader import image_reader, show_image im = "/home/joemarshal/Downloads/himan.png" mode = -1 img = image_reader(im, mode) # print(img.shape) height, width, *no_of_channels = img.shape print(height, width, no_of_channels) for h in range(height): for w in range(width): k = img[h][w] k = k[:3] img1[h][w] = k show_image(img) cv2.imwrite("trying.png", img)
""" Code to equalise images with histogram """ import numpy as np import cv2 from helpers.image_reader import image_reader, show_image if __name__ == "__main__": im = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = 0 img = image_reader(im, mode) show_image(img) mask = np.zeros(img.shape[:2], np.uint8) mask[82:328, 31:338] = 255 masked_img = cv2.bitwise_and(img, img, mask=mask) equal = cv2.equalizeHist(img) mask1 = cv2.equalizeHist(masked_img) res = np.hstack((img, equal, mask1)) show_image(res) cv2.imwrite("/home/joemarshal/image_processing_opencv/images/contrast_ronaldo.png", equal) cv2.imwrite("/home/joemarshal/image_processing_opencv/images/equal_hist.png", res)
import cv2 from helpers.image_reader import image_reader, show_image if __name__ == "__main__": img = "/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg" mode = cv2.WINDOW_NORMAL im = image_reader(img, mode) rows, columns = im.shape[:2] print(rows, columns) center = (rows / 2, columns / 2) rotating_image = cv2.getRotationMatrix2D(center, 570, 1.0) m = cv2.warpAffine(im, rotating_image, (columns, rows)) i = show_image(m)
h, w, *no_of_channels = cvt3.shape for y in range(h): for x in range(w): cvt3[y][x][2] = cvt3[y][x][2] + factor print(cvt3[y][x][2]) return cvt3 if __name__ == "__main__": im = "/home/joemarshal/image_processing_opencv/images/circles.png" mode = 1 img = image_reader(im, mode) cvt = bgr2hsv(img) factor = 100 show_image(cvt) # print(cvt[:, :, 0]) # res = increase_hue_value(cvt, factor) show_image(res) # res1 = increase_saturation_value(cvt, factor) # pprint(cvt) # print("-------------") # pprint(res1) show_image(res1) res2 = increase_value(cvt, factor) show_image(res2) #
""" Here you will see the code for thresholding an image """ import cv2 from helpers.image_reader import show_image if __name__ == "__main__": img = cv2.imread( '/home/joemarshal/image_processing_opencv/images/resizedronaldo.jpg', 0) ret, thresh_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) i = show_image(thresh_img) cv2.imwrite( "/home/joemarshal/image_processing_opencv/images/thresholdronaldo.jpg", thresh_img)
""" :param img1: pass image 1 :param img2: pass image 2 :return: returns the division of both the images """ return cv2.divide(img1, img2) if __name__ == "__main__": im1 = "/home/joemarshal/image_processing/images/resizedronaldo.jpg" im2 = "/home/joemarshal/image_processing/images/resizedmessi.jpg" mode = cv2.WINDOW_NORMAL image1 = image_reader(im1, mode) image2 = image_reader(im2, mode) show_image(image1) show_image(image2) bit_and = bitwise_and(image1, image2) bit_or = bitwise_or(image1, image2) bit_not = bitwise_not(image1, image2) bit_xor = bitwise_xor(image1, image2) subt = subtract(image1, image2) mul = multiplication(image1, image2) div = division(image1, image2) show_image(bit_and) show_image(bit_or) show_image(bit_not) show_image(bit_xor) show_image(subt) show_image(mul) show_image(div)