How to prepare image to recognize by tesseract OCR - delphi

I use Tesseract OCR to to extract meter reading... tesseract needs to recognize right white background and black numbers.. I tried to threshold image
src := cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE);
dst := cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvThreshold(src, dst, 50, 250, CV_THRESH_BINARY);
but i didn't get the right result.. what should I do?
I use deplhi6 with Delphi-OpenCV
https://github.com/Laex/Delphi-OpenCV

You can treat this image as follows:
for jy:= 0 to bm.Height do
for ix := 0 to bm.Width do
begin
cor:=bm.Canvas.Pixels[ix,jy];
R:=GetRValue(Cor);
G:=GetGValue(Cor);
B:=GetBValue(Cor);
if g>38 then
bm.Canvas.Pixels[ix,jy]:=clWhite
else
bm.Canvas.Pixels[ix,jy]:=clBlack;
end;
As an output I got the following image:
Hope this helps.

Related

Find people with GOCV

I worked last year with OpenCV and Python. Today I wanted to try OpenCV using Golang with the GOCV package. I just wanted a simple Python example () to evalute but in Golang. I used even the same parameters (except the hiThresh and finalThreshold, i used the default values). Somehow I cannot get it working with GOCV, he only finds one centered result.
Here is my code:
package main
import (
"encoding/json"
"fmt"
"image"
"image/color"
"gocv.io/x/gocv"
)
func main() {
// define default hog descriptor
hog := gocv.NewHOGDescriptor()
defer hog.Close()
hog.SetSVMDetector(gocv.HOGDefaultPeopleDetector())
// color for the rect when faces detected
blue := color.RGBA{0, 0, 255, 0}
// read image
img := gocv.IMRead("images/person_010.bmp", 0)
//resize image
fact := float64(400) / float64(img.Cols())
newY := float64(img.Rows()) * fact
gocv.Resize(img, img, image.Point{X: 400, Y: int(newY)}, 0, 0, 1)
// detect people in image
rects := hog.DetectMultiScaleWithParams(img, 0, image.Point{X: 8, Y: 8}, image.Point{X: 16, Y: 16}, 1.05, 2, false)
// print found points
printStruct(rects)
// draw a rectangle around each face on the original image,
// along with text identifing as "Human"
for _, r := range rects {
gocv.Rectangle(img, r, blue, 3)
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
gocv.PutText(img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
}
if ok := gocv.IMWrite("loool.jpg", img); !ok {
fmt.Println("Error")
}
}
func printStruct(i interface{}) {
b, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(b))
}
Here is the input image:
And here is the result:
Actually, I've just run the code you posted with the image you provided—and I've got another resulting image:
I'm running:
gocv version: 0.10.0
opencv lib version: 3.4.1

gocv: how to cut out an image from blue background using opencv

I started playing with gocv. I'm trying to figure out a simple thing: how to cut out an object from an image which has a background of certain colour. In this case the object is pizza and background colour is blue.
I'm using InRange function (inRange in OpenCV) to define the upper and lower threshold for blue colour to create a mask and then CopyToWithMask function (copyTo in OpenCV) to apply the mask on the original image. I expect the result to be the blue background with the pizza cut out of it.
The code is very simple:
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 %s\n", 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)
}
However the resulting image is basically almost completely black except for a slight hint of a pizza edge:
As for the chosen upper and lower bound of blue colours, I followed the guide mentioned in the official documentation:
blue = np.uint8([[[255, 0, 0]]])
hsv_blue = cv2.cvtColor(blue, cv2.COLOR_BGR2HSV)
print(hsv_blue)
[[[120 255 255]]]
Now you take [H-10, 100,100] and [H+10, 255, 255] as lower bound and
upper bound respectively.
I'm sure I'm missing something fundamental, but can't figure out what it is.
So I spent quite some time on this to figure out what I'm missing and finally found the answer to my question in case anyone is interested. It's now clearer to me now why this question hasn't been answered as the solution to it is rather crazy due to gocv API.
Here is the code that I had to write to get the result I'm after:
package main
import (
"fmt"
"os"
"path/filepath"
"gocv.io/x/gocv"
)
func main() {
// read image
pizzaPath := filepath.Join("pizza.png")
pizza := gocv.IMRead(pizzaPath, gocv.IMReadColor)
if pizza.Empty() {
fmt.Printf("Failed to read image: %s\n", pizzaPath)
os.Exit(1)
}
// Convert BGR to HSV image (dont modify the original)
hsvPizza := gocv.NewMat()
gocv.CvtColor(pizza, &hsvPizza, gocv.ColorBGRToHSV)
pizzaChannels, pizzaRows, pizzaCols := hsvPizza.Channels(), hsvPizza.Rows(), hsvPizza.Cols()
// define HSV color upper and lower bound ranges
lower := gocv.NewMatFromScalar(gocv.NewScalar(110.0, 50.0, 50.0, 0.0), gocv.MatTypeCV8UC3)
upper := gocv.NewMatFromScalar(gocv.NewScalar(130.0, 255.0, 255.0, 0.0), gocv.MatTypeCV8UC3)
// split HSV lower bounds into H, S, V channels
lowerChans := gocv.Split(lower)
lowerMask := gocv.NewMatWithSize(pizzaRows, pizzaCols, gocv.MatTypeCV8UC3)
lowerMaskChans := gocv.Split(lowerMask)
// split HSV lower bounds into H, S, V channels
upperChans := gocv.Split(upper)
upperMask := gocv.NewMatWithSize(pizzaRows, pizzaCols, gocv.MatTypeCV8UC3)
upperMaskChans := gocv.Split(upperMask)
// copy HSV values to upper and lower masks
for c := 0; c < pizzaChannels; c++ {
for i := 0; i < pizzaRows; i++ {
for j := 0; j < pizzaCols; j++ {
lowerMaskChans[c].SetUCharAt(i, j, lowerChans[c].GetUCharAt(0, 0))
upperMaskChans[c].SetUCharAt(i, j, upperChans[c].GetUCharAt(0, 0))
}
}
}
gocv.Merge(lowerMaskChans, &lowerMask)
gocv.Merge(upperMaskChans, &upperMask)
// global mask
mask := gocv.NewMat()
gocv.InRange(hsvPizza, lowerMask, upperMask, &mask)
// cut out pizza mask
pizzaMask := gocv.NewMat()
gocv.Merge([]gocv.Mat{mask, mask, mask}, &pizzaMask)
// cut out the pizza and convert back to BGR
gocv.BitwiseAnd(hsvPizza, pizzaMask, &hsvPizza)
gocv.CvtColor(hsvPizza, &hsvPizza, gocv.ColorHSVToBGR)
// write image to filesystem
outPizza := "no_pizza.jpeg"
if ok := gocv.IMWrite(outPizza, hsvPizza); !ok {
fmt.Printf("Failed to write image: %s\n", outPizza)
os.Exit(1)
}
// write pizza mask to filesystem
outPizzaMask := "no_pizza_mask.jpeg"
if ok := gocv.IMWrite(outPizzaMask, mask); !ok {
fmt.Printf("Failed to write image: %s\n", outPizza)
os.Exit(1)
}
}
This code produces the result I was after:
I'm also going to add another picture that shows the im
Now, let's get to code. gocv API function InRange() does not accept Scalar like OpenCV does so you have to do all that crazy image channel splitting and merging dance since you need to pass in Mats as lower and upper bounds to InRange(); these Mat masks have to have the exact number of channels as the image on which you run InRange().
This brings up another important point: when allocating the Scalars in gocv for this task, I originally used gocv.MatTypeCV8U type which represents single channel color - not enough for HSV image which has three channels -- this is fixed by using gocv.MatTypeCV8UC3 type.
If I it were possible pass in gocv.Scalars into gocv.InRange() a lot of the boiler plate code would disappear; so would all the unnecessary gocv.NewMat() allocations for splitting and reassembling the channels which are required to create lower and upper bounds channels.
inRange with the given range runs perfectly for me. I'm not familiar with Go, but here's my python code:
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()
A few of notes here:
inRange returns the blue background so we need to invert it to reveal the object's mask (if you need the object).
You don't need to apply mask on hsvImg and convert to BGR, you can apply mask directly on the original image (which is BGR already).
Python does not have CopyToWithMask so I use the equivalent bitwise_and. You may check this function in Go, but I suspect there would be no differences.
Here is what I did with Python because I don't know Go...
Let me explain first.
(1) Image has been turned to gray.
(2) Applied Canny Edge
(3 - 4) Created kernel and used it to do Dilate and Close operations
(5) Found contours
(6) Created and applied mask
(7) Cropped and saved the region
Here is the code:
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)
# if using OpenCV 4, remove image variable from below
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)
Hope will help more than one user.

thresholding an image with bright zones

I am developing an app for iOS with openCV that take a picture from a monitor and extract a curve, but when the image has some bright zones after thresholding, I don't get the complete curve but some black zones
Original image
processed image after thresholding
original = [MAOpenCV cvMatGrayFromUIImage:_sourceImage];
cv::threshold(original, original, 70, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
findContours(original, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
cv::Mat skel(original.size(), CV_8UC1, cv::Scalar(0));
int idx = 0;
for(; idx >= 0; idx = hierarchy[idx][0])
{
if (contours[idx].size()>250 && idx>-1){
cv::Scalar color( 255,255,255);
drawContours(skel, contours, idx, color, CV_FILLED, 8, hierarchy);
}
}
cv::threshold(skel, skel, 100, 255, CV_THRESH_BINARY_INV);
cv::erode(skel,skel,cv::Mat(),cv::Point(-1,-1),2);
So how I can process the image to extract the curve when the image have some bright zones like the example
When you have a background with an uneven illumination, you may want to apply first a White Top-Hat (or here for MatLab, and here for OpenCV).
Here is the result I got using a structuring element of type disk with radius 3.
Then, whatever thresholding method you choose will work.
Wouldn't be sufficient to use Otsu's thresholding?
Code fragment:
import cv2
image = cv2.imread('d:/so.jpg', cv2.IMREAD_GRAYSCALE)
threshold, thresholded = cv2.threshold(image, 0, 255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print(threshold)
cv2.imshow('so', image);
cv2.imshow('thresholded', thresholded)
cv2.waitKey(0)

Convert IPL_DEPTH_16S image in to IPL_DEPTH_8U in JavaCV

I have a One Image with depth of IPL_DEPTH_16S
IplImage result = cvCreateImage(cvGetSize(smoothImage), IPL_DEPTH_16S, 1);
cvSobel(smoothImage, result, 0, 1, 3);
and i want to pass that result image to other object which needs an IPL_DEPTH_8U image. So Is there any way to convert IPL_DEPTH_16S to IPL_DEPTH_8U in JavaCV.
I already try to use cvConvertScale() method. But i can't find what are the exact parametrs for that method.
Thankx..
Using the same style as your code, this should work:
IplImage i = cvCreateImage(cvGetSize(result), IPL_DEPTH_8U, 1);
cvConvertScale(result, i, 1, 0);

Mask out white color from PNG or GIF image, blit it to canvas using any color

Source is either PNG or GIF where the pixels that should be "colorized" are white. Background can be either black or transparent, whichever is easiest.
Now I'd like to cut out a rectangular part of the source, and AND it with the palette color (gif) or RGB color (png) of the "brush", to "stamp" it out on a TImage/TCanvas with that color.
Probably one of those lazy questions where RTFM would do. But if you have a nice solution please share :)
I tried Daud's PNGImage lib, but I can't even get it loading the source image. Is there a trick to using it?
The solution needs to work on D7 and up, XP and up.
do i understand you want to change the white color with some other color?
if that is so i think you should check the image pixel by pixel and check what color is the pixel and change it if is white.
thats how you can loop through image
var
iX : Integer;
Line: PByteArray;
...
Line := Image1.ScanLine[0]; // We are scanning the first line
iX := 0;
// We can't use the 'for' loop because iX could not be modified from
// within the loop
repeat
Line[iX] := Line[iX] - $F; // Red value
Line[iX + 1] := Line[iX] - $F; // Green value
Line[iX + 2] := Line[iX] - $F; // Blue value
Inc(iX, 3); // Move to next pixel
until iX > (Image1.Width - 1) * 3;
Here's code that show how to reads the Red and Blue values and switched them.
var
btTemp: Byte; // Used to swap colors
iY, iX: Integer;
Line : PByteArray;
...
for iY := 0 to Image1.Height - 1 do begin
Line := Image1.ScanLine[iY]; // Read the current line
repeat
btSwap := Line[iX]; // Save red value
Line[iX] := Line[iX + 2]; // Switch red with blue
Line[iX + 2] := btSwap; // Switch blue with previously saved red
// Line[iX + 1] - Green value, not used in example
Inc(iX, 3);
until iX > (Image1.Width - 1) * 3;
end;
Image1.Invalidate; // Redraw bitmap after everything's done
but this is for bitmap image only.
if this is useful try to convert your image to bitmap and from then manipulate it.

Resources