Reputation: 103
I want to extract only the signature part from the image and save it.
I have successfully implemented it in Python using the OpenCv library.
But OpenCv is not available in Ruby.
I have tried all wrappers of OpenCv for Ruby. They didn't work e.g. https://github.com/ruby-opencv/ruby-opencv .
Below is my code for the image processing:
import numpy as np
import cv2
image = cv2.imread('10.jpg')
result = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([90, 38, 0])
upper = np.array([145, 255, 255])
mask = cv2.inRange(image, lower, upper)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
boxes = []
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
boxes.append([x,y, x+w,y+h])
boxes = np.asarray(boxes)
left = np.min(boxes[:,0])
top = np.min(boxes[:,1])
right = np.max(boxes[:,2])
bottom = np.max(boxes[:,3])
result[close==0] = (255,255,255)
ROI = result[top:bottom, left:right].copy()
cv2.rectangle(result, (left,top), (right,bottom), (36, 255, 12), 2)
cv2.imshow('result', result)
cv2.imshow('ROI', ROI)
cv2.imshow('close', close)
cv2.imwrite('result.png', result)
cv2.imwrite('ROI.png', ROI)
cv2.waitKey()
I have even tried image processing using gem imagemagick, but it didn't work.
How can I code in Ruby to do same work of above code.
Upvotes: 3
Views: 795
Reputation: 1891
this works for me for your image (saved as sig.jpg
), output in out.jpg
Install rmagick:
sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev
sudo gem install rmagick
Ruby code:
require 'rmagick'
include Magick
image = ImageList.new("sig.jpg")
image.fuzz = '40%'
image.trim!
image.write("out.jpg")
Upvotes: 2