Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- velo = cv2.imread("velo.jpg")
- cosmo = cv2.imread("cosmo.jpg")
- cheshir = cv2.imread("cot.jpg")
- furie = cv2.imread("detail.jpg", cv2.IMREAD_GRAYSCALE)
- people = cv2.imread("people.jpg")
- fotik = cv2.imread("fotik.jpg")
- n = input("Какой номер показываем? ")
- if n == "1":
- cheshir_canny = cheshir.copy()
- highTh = 50
- lowTh1 = highTh / 1.5
- cheshir_canny1 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
- lowTh2 = highTh / 2.75
- cheshir_canny2 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
- lowTh3 = highTh / 4
- cheshir_canny3 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
- cv2.imshow("original", cheshir)
- cv2.imshow("canny 50 1.5", cheshir_canny1)
- cv2.imshow("canny 50 2.75", cheshir_canny2)
- cv2.imshow("canny 50 4", cheshir_canny3)
- highTh = 75
- lowTh1 = highTh / 1.5
- cheshir_canny4 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
- lowTh2 = highTh / 2.75
- cheshir_canny5 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
- lowTh3 = highTh / 4
- cheshir_canny6 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
- cv2.imshow("canny 75 1.5", cheshir_canny4)
- cv2.imshow("canny 75 2.75", cheshir_canny5)
- cv2.imshow("canny 75 4", cheshir_canny6)
- highTh = 125
- lowTh1 = highTh / 1.5
- cheshir_canny7 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
- lowTh2 = highTh / 2.75
- cheshir_canny8 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
- lowTh3 = highTh / 4
- cheshir_canny9 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
- cv2.imshow("canny 125 1.5", cheshir_canny7)
- cv2.imshow("canny 125 2.75", cheshir_canny8)
- cv2.imshow("canny 125 4", cheshir_canny9)
- highTh = 175
- lowTh1 = highTh / 1.5
- cheshir_canny10 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
- lowTh2 = highTh / 2.75
- cheshir_canny11 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
- lowTh3 = highTh / 4
- cheshir_canny12 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
- cv2.imshow("canny 175 1.5", cheshir_canny10)
- cv2.imshow("canny 175 2.75", cheshir_canny11)
- cv2.imshow("canny 175 4", cheshir_canny12)
- highTh = 225
- lowTh1 = highTh / 1.5
- cheshir_canny13 = cv2.Canny(cheshir_canny, lowTh1, highTh, apertureSize=3)
- lowTh2 = highTh / 2.75
- cheshir_canny14 = cv2.Canny(cheshir_canny, lowTh2, highTh, apertureSize=3)
- lowTh3 = highTh / 4
- cheshir_canny15 = cv2.Canny(cheshir_canny, lowTh3, highTh, apertureSize=3)
- cv2.imshow("canny 225 1.5", cheshir_canny10)
- cv2.imshow("canny 225 2.75", cheshir_canny11)
- cv2.imshow("canny 225 4", cheshir_canny12)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "2":
- gray = cv2.cvtColor(velo, cv2.COLOR_BGR2GRAY)
- edges = cv2.Canny(gray, 50, 150, apertureSize=3)
- cv2.imshow('edges', edges)
- lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
- print(lines)
- for line in lines:
- rho, theta = line[0]
- a = np.cos(theta)
- b = np.sin(theta)
- x0 = a * rho
- y0 = b * rho
- # x1 stores the rounded off value of (r * cos(theta) - 1000 * sin(theta))
- x1 = int(x0 + 2000 * (-b))
- # y1 stores the rounded off value of (r * sin(theta)+ 1000 * cos(theta))
- y1 = int(y0 + 2000 * a)
- # x2 stores the rounded off value of (r * cos(theta)+ 1000 * sin(theta))
- x2 = int(x0 - 2000 * (-b))
- # y2 stores the rounded off value of (r * sin(theta)- 1000 * cos(theta))
- y2 = int(y0 - 2000 * a)
- cv2.line(velo, (x1, y1), (x2, y2), (0, 0, 255), 2)
- cv2.imshow('image', velo)
- gray_img = cv2.cvtColor(cosmo, cv2.COLOR_BGR2GRAY)
- img = cv2.medianBlur(gray_img, 5)
- cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
- # center
- circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 120, param1=100, param2=30, minRadius=0, maxRadius=0)
- circles = np.uint16(np.around(circles))
- for i in circles[0, :]:
- cv2.circle(cosmo, (i[0], i[1]), i[2], (0, 255, 0), 6)
- cv2.circle(cosmo, (i[0], i[1]), 2, (0, 0, 255), 3)
- cv2.imshow("HoughCirlces", cosmo)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "3":
- cv2.imshow("orig", furie)
- fft_image = np.fft.fft2(furie)
- fft_shift_image = np.fft.fftshift(fft_image)
- magnitude_spectrum = 20 * np.log(np.abs(fft_shift_image))
- magnitude_spectrum = np.asarray(magnitude_spectrum, dtype=np.uint8)
- normal = np.random.randint(-4, 6, 1)
- # Esli vidast 0+0j proga budet lomat'sya
- fft_normal = np.fft.fft(normal)
- multi = fft_normal * fft_image
- inverse_multi = np.fft.ifft2(multi)
- magnitude_spectrum_inverse_muliti = 20 * np.log((np.abs(inverse_multi) + 1))
- magnitude_spectrum_inverse_muliti = np.asarray(magnitude_spectrum_inverse_muliti, dtype=np.uint8)
- speech = "inverse fft with: " + str(fft_normal)
- cv2.imshow(speech, magnitude_spectrum_inverse_muliti)
- cv2.imshow("fft", magnitude_spectrum)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "4":
- img = cv2.imread("scene.jpg", cv2.IMREAD_GRAYSCALE)
- img1 = img.copy()
- cv2.imshow("original", img1)
- print(img.shape)
- intIMAGE = cv2.integral(img)
- MaxFormatValue = 255
- koeff = MaxFormatValue / intIMAGE[intIMAGE.shape[0] - 1][intIMAGE.shape[1] - 1]
- integral = intIMAGE * (koeff)
- npINTEG = np.asarray(integral)
- print("Viberite oblast izobrazheniya:")
- x = int(input())
- y = int(input())
- diff_image = cv2.Sobel(npINTEG[x:, :y], -1, 1, 1)
- cv2.imshow("original1212", diff_image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "5":
- image = cv2.imread("ugol.jpg", cv2.IMREAD_GRAYSCALE)
- print(image.shape)
- print("x1,y1,x2,y2:")
- y1 = int(input())
- x1 = int(input())
- y2 = int(input())
- x2 = int(input())
- new_image = image[y1:y2, x1:x2]
- Non, Non, titled = cv2.integral3(new_image)
- cv2.imshow("new image with new x and y", new_image)
- integral_45 = titled
- print(integral_45)
- MaxFormatValue = 255
- koeff = MaxFormatValue / integral_45[integral_45.shape[0] - 1][integral_45.shape[1] - 1]
- print(koeff)
- integral_45 = integral_45 * (koeff)
- print(integral_45)
- cv2.imshow("int", integral_45)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "6":
- cv2.imshow("original", people)
- people1 = people.copy()
- gray = cv2.cvtColor(people, cv2.COLOR_BGR2GRAY)
- blur = cv2.GaussianBlur(gray, (1, 1), 1000)
- flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
- # Find contours
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- contours = sorted(contours, key=cv2.contourArea, reverse=True)
- # Select long perimeters only
- perimeters = [cv2.arcLength(contours[i], True) for i in range(len(contours))]
- listindex = [i for i in range(15) if perimeters[i] > perimeters[0] / 2]
- numcards = len(listindex)
- # Show image
- imgcont = people.copy()
- [cv2.drawContours(imgcont, [contours[i]], 0, (255, 255, 255), 5) for i in listindex]
- # cv2.imshow("contur", imgcont)
- mask = np.zeros(people.shape[: 2], np.uint8)
- mask1 = people - imgcont
- r, thresh1 = cv2.threshold(mask1, 128, 255, cv2.THRESH_BINARY)
- # cv2.imshow("mask", thresh1)
- bgdModel = np.zeros((1, 65), np.float64)
- fgdModel = np.zeros((1, 65), np.float64)
- # mask[mask1 == 0] = 0
- # mask[mask1 == 255] = 1
- rect = (460, 10, 1200, 800)
- cv2.grabCut(people, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
- mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype("uint8")
- people = people * mask2[:, :, np.newaxis]
- cv2.imshow("catting", people)
- fon = people1 - people
- cv2.imshow("catting1", fon)
- dst1 = cv2.cvtColor(people, cv2.COLOR_BGR2GRAY)
- r, g, b = cv2.split(people)
- dst = cv2.inpaint(fon, r, 3, cv2.INPAINT_TELEA)
- cv2.imshow("paint", dst)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- if n == "7":
- masked = np.zeros(fotik.shape[: 2], np.uint8)
- # r, g, b = cv2.split(fotik)
- img1 = cv2.pyrMeanShiftFiltering(src=fotik, sp=20, sr=45, dst=3)
- #img1 = cv2.floodFill(img)
- #cv2.imshow("seff", img1)
- cv2.imshow("original", fotik)
- cv2.imshow("seg1", img1)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement