Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os"
- "gocv.io/x/gocv"
- )
- func main() {
- imgPath := "pizza.png"
- // read in an image from filesystem
- img := gocv.IMRead(imgPath, gocv.IMReadColor)
- if img.Empty() {
- fmt.Printf("Could not read image %sn", imgPath)
- os.Exit(1)
- }
- // Create a copy of an image
- hsvImg := img.Clone()
- // Convert BGR to HSV image
- gocv.CvtColor(img, hsvImg, gocv.ColorBGRToHSV)
- lowerBound := gocv.NewMatFromScalar(gocv.NewScalar(110.0, 100.0, 100.0, 0.0), gocv.MatTypeCV8U)
- upperBound := gocv.NewMatFromScalar(gocv.NewScalar(130.0, 255.0, 255.0, 0.0), gocv.MatTypeCV8U)
- // Blue mask
- mask := gocv.NewMat()
- gocv.InRange(hsvImg, lowerBound, upperBound, mask)
- // maskedImg: output array that has the same size and type as the input arrays.
- maskedImg := gocv.NewMatWithSize(hsvImg.Rows(), hsvImg.Cols(), gocv.MatTypeCV8U)
- hsvImg.CopyToWithMask(maskedImg, mask)
- // save the masked image
- newImg := gocv.NewMat()
- // Convert back to BGR before saving
- gocv.CvtColor(maskedImg, newImg, gocv.ColorHSVToBGR)
- gocv.IMWrite("no_pizza.jpeg", newImg)
- }
- blue = np.uint8([[[255, 0, 0]]])
- hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
- print(hsv_blue)
- [[[120 255 255]]]
- import cv2
- import numpy as np
- image = cv2.imread("image.png")
- copy = image.copy()
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- cv2.imshow('Gray', gray)
- cv2.waitKey(0)
- edged = cv2.Canny(gray, 10, 250)
- cv2.imshow('Edged', edged)
- cv2.waitKey(0)
- kernel = np.ones((5, 5), np.uint8)
- dilation = cv2.dilate(edged, kernel, iterations=1)
- cv2.imshow('Dilation', dilation)
- cv2.waitKey(0)
- closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
- cv2.imshow('Closing', closing)
- cv2.waitKey(0)
- (image, cnts, hiers) = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- cont = cv2.drawContours(copy, cnts, -1, (0, 0, 0), 1, cv2.LINE_AA)
- cv2.imshow('Contours', cont)
- cv2.waitKey(0)
- mask = np.zeros(cont.shape[:2], dtype="uint8") * 255
- # Draw the contours on the mask
- cv2.drawContours(mask, cnts, -1, (255, 255, 255), -1)
- # remove the contours from the image and show the resulting images
- img = cv2.bitwise_and(cont, cont, mask=mask)
- cv2.imshow("Mask", img)
- cv2.waitKey(0)
- for c in cnts:
- x, y, w, h = cv2.boundingRect(c)
- if w > 50 and h > 130:
- new_img = img[y:y + h, x:x + w]
- cv2.imwrite('Cropped.png', new_img)
- cv2.imshow("Cropped", new_img)
- cv2.waitKey(0)
- import numpy as py
- import cv2
- img = cv2.imread("pizza.png")
- hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
- mask = cv2.inRange(hsv, (110, 100, 100), (130, 255, 255))
- inv_mask = cv2.bitwise_not(mask)
- pizza = cv2.bitwise_and(img, img, mask=inv_mask)
- cv2.imshow("img", img)
- cv2.imshow("mask", mask)
- cv2.imshow("pizza", pizza)
- cv2.imshow("inv mask", inv_mask)
- cv2.waitKey()
Add Comment
Please, Sign In to add comment