Created
November 3, 2017 08:22
-
-
Save nooperpudd/5491d68ce98f33d3e60d643ea69665f8 to your computer and use it in GitHub Desktop.
opencv mask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy | |
image = cv2.imread("test.png") | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert image to gray | |
# cv2.imshow("image",gray) | |
blur_image = cv2.GaussianBlur(gray, (5, 5), 0) # 平滑图像处理,高斯滤波 | |
edged = cv2.Canny(blur_image, 180, 900) # Canny方法对图像进行边缘检测 | |
# cv2.imshow("Image", edged) | |
# cv2.waitKey(0) | |
# applying close function | |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40, 40)) | |
# opened = cv2.morphologyEx(edged, cv2.MORPH_OPEN, kernel) | |
close = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel) | |
dilated = cv2.dilate(close, None, iterations=2) | |
cv2.imshow("Closed", dilated) | |
cv2.waitKey(0) | |
_, contours, hierarchy = cv2.findContours(close.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
# # work files | |
# cv2.drawContours(image, contours, -1, (0, 0, 255), 3) | |
# # #绘制结果 | |
# cv2.imshow("result", image) | |
# cv2.drawContours(image, contours, -1, (0, 0, 255), 3) | |
# 绘制结果 | |
# cv2.imshow("result", image) | |
# mask = numpy.full(image.shape[:2], 255, dtype=numpy.uint8) # white space | |
# mask = numpy.zeros_like(image) | |
# cv2.imshow("mask",mask) | |
# cv2.drawContours(mask, contours, 0, 255, 1) | |
# print(type(mask)) | |
# print(mask.shape) | |
# print(mask.ndim) | |
# print(mask.dtype) | |
# | |
# cv2.imshow("ss", mask) | |
result_image = cv2.bitwise_and(image,image,mask=dilated) | |
# # print(mask) | |
cv2.imshow("hh",result_image) | |
# cv2.drawContours(mask, contours, 0, 255, -1) | |
# # | |
# cv2.imshow("5",result_image) | |
# # | |
# out = numpy.zeros_like(image) | |
# out[mask == 255] = image[mask == 255] | |
# cv2.imshow("output", out) | |
# cv2.waitKey(0) | |
# mask = np.ones((image.height,image.width),dtype=np.uint8) | |
# m#ask[:,:]= 0 | |
# croped = np.zeros() | |
# // you could also reuse img1 here | |
# Mat mask = Mat::zeros(img1.rows, img1.cols, CV_8UC1); | |
# | |
# // CV_FILLED fills the connected components found | |
# drawContours(mask, contours, -1, Scalar(255), CV_FILLED); | |
# | |
# /* | |
# Before drawing all contours you could also decide | |
# to only draw the contour of the largest connected component | |
# found. Here's some commented out code how to do that: | |
# */ | |
# | |
# // vector<double> areas(contours.size()); | |
# // for(int i = 0; i < contours.size(); i++) | |
# // areas[i] = contourArea(Mat(contours[i])); | |
# // double max; | |
# // Point maxPosition; | |
# // minMaxLoc(Mat(areas),0,&max,0,&maxPosition); | |
# // drawContours(mask, contours, maxPosition.y, Scalar(1), CV_FILLED); | |
# | |
# // let's create a new image now | |
# Mat crop(img0.rows, img0.cols, CV_8UC3); | |
# | |
# // set background to green | |
# crop.setTo(Scalar(0,255,0)); | |
# | |
# // and copy the magic apple | |
# img0.copyTo(crop, mask); | |
# idx = 0 | |
# | |
# mask = np.zeros_like(image) # Create mask where white is what we want, black otherwise | |
# cv2.drawContours(mask, contours, idx, 255, -1) # Draw filled contour in mask | |
# out = np.zeros_like(image) # Extract out the object and place into output image | |
# out[mask == 255] = image[mask == 255] | |
# | |
# # Show the output image | |
# cv2.imshow('Output', out) | |
# cv2.waitKey(0) | |
# for c in contours: | |
# x,y,w,h = cv2.boundingRect(c) | |
# if w>50 and h>50: | |
# idx+=1 | |
# new_img=image[y:y+h,x:x+w] | |
# cv2.imwrite(str(idx) + '.png', new_img) | |
# cv2.imshow("im",image) | |
# cv2.imshow("Show Boxes", image) | |
# for c in cnts: | |
# peri = cv2.arcLength(c, True) | |
# approx = cv2.approxPolyDP(c, 0.02 * peri, True) | |
# cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) | |
# cv2.imshow("Output", image) | |
cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment