Guest User

Untitled

a guest
Mar 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "os"
  6.  
  7. "gocv.io/x/gocv"
  8. )
  9.  
  10. func main() {
  11. imgPath := "pizza.png"
  12. // read in an image from filesystem
  13. img := gocv.IMRead(imgPath, gocv.IMReadColor)
  14. if img.Empty() {
  15. fmt.Printf("Could not read image %sn", imgPath)
  16. os.Exit(1)
  17. }
  18. // Create a copy of an image
  19. hsvImg := img.Clone()
  20.  
  21. // Convert BGR to HSV image
  22. gocv.CvtColor(img, hsvImg, gocv.ColorBGRToHSV)
  23. lowerBound := gocv.NewMatFromScalar(gocv.NewScalar(110.0, 100.0, 100.0, 0.0), gocv.MatTypeCV8U)
  24. upperBound := gocv.NewMatFromScalar(gocv.NewScalar(130.0, 255.0, 255.0, 0.0), gocv.MatTypeCV8U)
  25.  
  26. // Blue mask
  27. mask := gocv.NewMat()
  28. gocv.InRange(hsvImg, lowerBound, upperBound, mask)
  29.  
  30. // maskedImg: output array that has the same size and type as the input arrays.
  31. maskedImg := gocv.NewMatWithSize(hsvImg.Rows(), hsvImg.Cols(), gocv.MatTypeCV8U)
  32. hsvImg.CopyToWithMask(maskedImg, mask)
  33.  
  34. // save the masked image
  35. newImg := gocv.NewMat()
  36. // Convert back to BGR before saving
  37. gocv.CvtColor(maskedImg, newImg, gocv.ColorHSVToBGR)
  38. gocv.IMWrite("no_pizza.jpeg", newImg)
  39. }
  40.  
  41. blue = np.uint8([[[255, 0, 0]]])
  42. hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
  43. print(hsv_blue)
  44.  
  45. [[[120 255 255]]]
  46.  
  47. import cv2
  48. import numpy as np
  49.  
  50. image = cv2.imread("image.png")
  51. copy = image.copy()
  52.  
  53. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  54. cv2.imshow('Gray', gray)
  55. cv2.waitKey(0)
  56.  
  57. edged = cv2.Canny(gray, 10, 250)
  58. cv2.imshow('Edged', edged)
  59. cv2.waitKey(0)
  60.  
  61. kernel = np.ones((5, 5), np.uint8)
  62.  
  63. dilation = cv2.dilate(edged, kernel, iterations=1)
  64. cv2.imshow('Dilation', dilation)
  65. cv2.waitKey(0)
  66.  
  67. closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  68. cv2.imshow('Closing', closing)
  69. cv2.waitKey(0)
  70.  
  71. (image, cnts, hiers) = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  72.  
  73. cont = cv2.drawContours(copy, cnts, -1, (0, 0, 0), 1, cv2.LINE_AA)
  74. cv2.imshow('Contours', cont)
  75. cv2.waitKey(0)
  76.  
  77. mask = np.zeros(cont.shape[:2], dtype="uint8") * 255
  78.  
  79. # Draw the contours on the mask
  80. cv2.drawContours(mask, cnts, -1, (255, 255, 255), -1)
  81.  
  82. # remove the contours from the image and show the resulting images
  83. img = cv2.bitwise_and(cont, cont, mask=mask)
  84. cv2.imshow("Mask", img)
  85. cv2.waitKey(0)
  86.  
  87. for c in cnts:
  88. x, y, w, h = cv2.boundingRect(c)
  89. if w > 50 and h > 130:
  90. new_img = img[y:y + h, x:x + w]
  91. cv2.imwrite('Cropped.png', new_img)
  92.  
  93. cv2.imshow("Cropped", new_img)
  94. cv2.waitKey(0)
  95.  
  96. import numpy as py
  97. import cv2
  98.  
  99. img = cv2.imread("pizza.png")
  100.  
  101. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  102. mask = cv2.inRange(hsv, (110, 100, 100), (130, 255, 255))
  103. inv_mask = cv2.bitwise_not(mask)
  104.  
  105. pizza = cv2.bitwise_and(img, img, mask=inv_mask)
  106.  
  107. cv2.imshow("img", img)
  108. cv2.imshow("mask", mask)
  109. cv2.imshow("pizza", pizza)
  110. cv2.imshow("inv mask", inv_mask)
  111. cv2.waitKey()
Add Comment
Please, Sign In to add comment