Crop Rectangle returned by minAreaRect OpenCV [Python]

here a function that does this task: import cv2 import numpy as np def crop_minAreaRect(img, rect): # rotate img angle = rect[2] rows,cols = img.shape[0], img.shape[1] M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1) img_rot = cv2.warpAffine(img,M,(cols,rows)) # rotate bounding box rect0 = (rect[0], rect[1], 0.0) box = cv2.boxPoints(rect0) pts = np.int0(cv2.transform(np.array([box]), M))[0] pts[pts < 0] = 0 # crop … Read more

Using OpenCV with Tkinter

This should work: import numpy as np import cv2 import Tkinter as tk import Image, ImageTk #Set up GUI window = tk.Tk() #Makes main window window.wm_title(“Digital Microscope”) window.config(background=”#FFFFFF”) #Graphics window imageFrame = tk.Frame(window, width=600, height=500) imageFrame.grid(row=0, column=0, padx=10, pady=2) #Capture video frames lmain = tk.Label(imageFrame) lmain.grid(row=0, column=0) cap = cv2.VideoCapture(0) def show_frame(): _, frame = … Read more

Filling holes inside a binary object

There are two methods to do this: 1) Contour Filling: First, invert the image, find contours in the image, fill it with black and invert back. des = cv2.bitwise_not(gray) contour,hier = cv2.findContours(des,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: cv2.drawContours(des,[cnt],0,255,-1) gray = cv2.bitwise_not(des) Resulting image: 2) Image Opening: kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) res = cv2.morphologyEx(gray,cv2.MORPH_OPEN,kernel) The resulting image is … Read more

OpenCV CV::Mat and Eigen::Matrix

You can also use void eigen2cv(const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, Mat& dst) and void cv2eigen(const Mat& src, Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst) from #include <opencv2/core/eigen.hpp>.

Convert RGB to black OR white

Scaling to Black and White Convert to grayscale and then scale to white or black (whichever is closest). Original: Result: Pure Pillow implementation Install pillow if you haven’t already: $ pip install pillow Pillow (or PIL) can help you work with images effectively. from PIL import Image col = Image.open(“cat-tied-icon.png”) gray = col.convert(‘L’) bw = … Read more