OPENCV - How to use Haar cascade Github xml files in OpenCV? - c++

I am starting to learn object detection in OpenCV 3.4.2 (.Net C++ 2017).
I am very interested in detecting strawberries in pictures (at the moment I am really interested in detecting just strawberries). I know OpenCV has some pre-trained Haar cascade files in OpenCV directory, but there are not .xml files for strawberries (there are for body parts instead).
So I decided to search on Google, to try to find trained strawberry Haar cascade .xml files. I found this .xml file XML strawberry file but I get error -49 when I try to execute the program. I have executed correctly the program using OpenCV files, but I cannot execute correctly when I try with GitHub XML file.
I have found this thread here on StackOverflow StackOverFlow thread about GitHub XML files in OpenCV and a user claims that it's not possible to use GitHub XML files into OpenCV.
My question is about if there is a way to use the XML GitHub file I have posted in this thread in OpenCV or I need to train my own XML file? I would like to use GitHub file.
Edit(1)
I have found this link strawberry detection in OpenCV where, if you look at the source code, it seems that the same strawberry_classifier.xml is being used. I don't know if the name of the file is just a coincidence (Github filename and the filename shown in the source code of the 3rd link are exactly the same). At least it seems that the programmer (from the 3rd link) has obtained some results while using the (apparently) same .xml file that I want to use. But I don't know how to use that strawberry_classifier.xml file.

Python dev here,
I'm late, but in case anyone still wants to see an answer:
The classifier from GitHub works perfectly fine as shown in this Python code (Sorry, I didn't do it in C++, but I think it won't be much different)
The script uses your webcam as the image source. You can show the webcam some images of strawberries, and it will recognize it:
import cv2 #import library
#define Haar Cascade Classifier
Strawberry_Classifier = cv2.CascadeClassifier(r"C:\Users\Strawberry.xml")
VideoCapture = cv2.VideoCapture(0) #capture video from camera
#set video size
VideoCapture.set(3, 540)
VideoCapture.set(4, 360)
while True:
#Connect video and convert
Connection_Success, Video = VideoCapture.read() #returns a bool and video array in one tuple (sucess, video array)
RGB_video = cv2.cvtColor(Video, cv2.COLOR_BGR2RGB) #converts to suitable format
Detect_Strawberry = Strawberry_Classifier.detectMultiScale(RGB_video, 1.3, 13) #MODIFY THIS FOR LESS/MORE DETECTION ACCURACY
#Detect Strawberry
for(x,y,w,h) in Detect_Strawberry: #x,y width, height
cv2.rectangle(Video, (x, y), (x + w, y + h), (0, 255, 0), 3) #Put a rectangle around Strawberry
cv2.imshow("Window", Video) #show video
#quit if q is pressed, quit
QuitKey = cv2.waitKey(30)
if QuitKey == ord("q"):
VideoCapture.release()
cv2.destroyAllWindows()

Related

how to combine (merge) trainded data of Tesseract files

I have trained new font for english language with Tesseract OCR (JavaTessBoxEditor)
I received:
eng.trainddata
inttemp
normproto
pffmtable
shapetable
unicharset
files
I've tried to make combine_data, but nothing changed to main eng.traindeddata file. It was not not amended.
Then I've tried to download 3rd Party Software (QT version from Zdenko, GUI from General Delopment NL)
No result still
I ve searched all forums and youtube vids, no a clue how to combine main eng file and new eng trained file.
if someone want and can help me, msg kapabahwuk#yahoo.com.
I can pay for your time

Opencv Mosaic from video

I need to do the same thing of this video https://youtu.be/59RJeLlDAxQ but in Opencv. For now I'm doing this thing http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/ with a little modify in the final image merging, but it doesn't work very well. How can I proceed?
EDIT
For testing I'm using the video lab from this page http://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
I ran my code on that video and I obtain this:
It's not very accurate but its ok.If I let the program run, at a certain point my stitcher produce this:
.
For the stitching ROIs instead of the ramsrigoutham.com ones I'm using this:
warpPerspective(current_frame, rImg, H, Size(current_frame.cols, current_frame.rows), INTER_NEAREST);
Mat roi1(final_img, Rect(img_loop.cols, img_loop.rows, vImg[1].cols, vImg[1].rows));
Mat roi2(final_img, Rect(img_loop.cols, img_loop.rows, rImg.cols, rImg.rows));
rImg.copyTo(roi2);
vImg[1].copyTo(roi1);
Why not using: http://docs.opencv.org/modules/stitching/doc/high_level.html#stitcher-composepanorama
It's available on 2.4.11 and 3.0.0.
The link you mentioned is access deniedhttp://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
.
what are 'img_loop' and 'Vimg' and 'rimg' in your code? there is some difference between your code and the code you linked.if it is possible explain a little so I can work on your problem , cause Im doing the same thing in opencv

CImg Error : 'gm.exe' is not recognized as an internal or external command,

I am new to c++ programming , today i was trying to save an image using CImg .
CImg is C++ Template Image Processing Library .
The basic code i wrote is(Please forgive any syntax erros , as copied part of my codes) :
#include "CImg.h"// Include CImg library header.
#include <iostream>
using namespace cimg_library;
using namespace std;
const int screen_size = 800;
//-------------------------------------------------------------------------------
// Main procedure
//-------------------------------------------------------------------------------
int main()
{
CImg<unsigned char> img(screen_size,screen_size,1,3,20);
CImgDisplay disp(img, "CImg Tutorial");
//Some drawing using img.draw_circle( 10, 10, 60, RED);
img.save("result.jpg"); // save the image
return 0;
}
But I cannot run my program as it says :
Invalid Parameter - 100%
'gm.exe' is not recognized as an internal or external command,
operable program or batch file.
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
[CImg] *** CImgIOException *** [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.
terminate called after throwing an instance of 'cimg_library::CImgIOException'
what(): [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.
Though i can see the image , I cannot save it.
After googling a bit i found people saying to install ImageMagick , i have installed it but no help .
Some of the Forum says to compile against libjpeg, libpng, libmagick++. But i don't know how to compile against those libraries.
I am using Eclipse CDT plugin to write C++ project .
Please help me .
I had the same error, and installing of GraphicsMagick (not ImageMagick) helped me.
I've downloaded and installed GraphicsMagick-1.3.26-Q8-win64-dll.exe from ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/windows/. You may choose another one, if you need:
Note that the QuantumDepth=8 version (Q8) which provides industry
standard 24/32 bit pixels consumes half the memory and about 30% less
CPU than the QuantumDepth=16 version (Q16) which provides 48/64 bit
pixels for high-resolution color. A Q8 version is fine for processing
typical photos intended for viewing on a computer screen. If you are
dealing with film, scientific, or medical images, use ICC color
profiles, or deal with images that have limited contrast, then the Q16
version is recommended.
Important: during installation, don't remove checkbox "Update executable search path", which updates environment variable %PATH%, making gm.exe available from any place.
In my case, it was also required to install Ghostscript - which is highly recommended to install by GraphicsMagick. There is a link to x64 Ghostscript: https://sourceforge.net/projects/ghostscript/files/GPL%20Ghostscript/9.09/gs909w64.exe/download (I've put it here, because links from the GraphicMagick websites leads you to 32-bit only).
After that, it worked fine for me.
For some image formats (as .jpg, .png, .tif and basically all formats that require data compression), CImg will try to use an external tool to save them (such as convert from ImageMagick or gm from GraphicsMagick).
If you don't have any installed, then you won't be able to save .jpg files without having to link your code with the libjpeg library, to get a native support for JPEG read/write (then, you'll need to #define cimg_use_jpeg before #include "CImg.h", to tell the library you want to use the libjpeg features).
If you want to keep things simpler, I'd recommend to save your image using another (non-compressed) image format, as .bmp or .ppm.
These formats are handled natively by CImg and do not require to link with external libraries.
I know this question is old, but I kept getting the same error on one project and not on another and this is the only thing on Google.
To get rid of it, you must do 2 things:
Install dynamic ImageMagick libraries for your appropriate OS and architecture(32/64). Link
I was using VisualStudio, and the character set must be set to "Unicode". The error would appear again when I reverted back to Multi-Byte character set. I guess this has something to do with the way CImg handles strings and miscompares them.

View a sequence of images using Python and NumPy

I am using python's pil library to display images. Now I have a sequence of frames to display as a video content. I have a np.array that contains the RGB values of all the frames.
Could not find a method similar to Mathlabs implay to display these frames.
I can use imshow in a loop, but thats would be very slow as I need to mention framerate.
Matplotlib animations work well, and is easy to use. For reasonable size images they typically run at 30fps, or around that. Matplotlib 1.1+ has a nice new animation interface: here are some examples and a tutorial.
Older versions of matplotlib aren't to hard to animate either (you basically just set the data directly and refresh the plot) but the animation depends a bit more on the backend, so you need to look for an appropriate example.
For a specific example, if images is your list of matplotlib images that you want to animate, you can simply do:
animation.ArtistAnimation(fig, images, interval=50, blit=True, repeat_delay=1000)
This, btw, is taken from this example, if you want to also see the code that generates test images. The code to animate is simply the line above.
I have implemented a handy script that just suits your need. Try it out here
An example to show lists of images in a directory will be
import os
import glob
from scipy.misc import imread
img_dir = 'YOUR-IMAGE-DIRECTORY'
img_files = glob.glob(os.path.join(video_dir, '*.jpg'))
def redraw_fn(f, axes):
img_file = img_files[f]
img = imread(img_file)
if not redraw_fn.initialized:
redraw_fn.im = axes.imshow(img, animated=True)
redraw_fn.initialized = True
else:
redraw_fn.im.set_array(img)
redraw_fn.initialized = False
videofig(len(img_files), redraw_fn, play_fps=30)
If you happen to already have a working OpenCV install built with OpenEXR – which if you don’t, it’s at least as much of an irritating time-sink to rebuild OpenCV as e.g. compiling SciPy from source in the first place – but so if the library is already there and working, you can use the Python bindings to quickly† view images with only a little bit of boilerplate. From this example:
import OpenEXR, Imath, cv
filename = "GoldenGate.exr"
exrimage = OpenEXR.InputFile(filename)
dw = exrimage.header()['dataWindow']
(width, height) = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
def fromstr(s):
mat = cv.CreateMat(height, width, cv.CV_32FC1)
cv.SetData(mat, s)
return mat
pt = Imath.PixelType(Imath.PixelType.FLOAT)
(r, g, b) = [fromstr(s) for s in exrimage.channels("RGB", pt)]
bgr = cv.CreateMat(height, width, cv.CV_32FC3)
cv.Merge(b, g, r, None, bgr)
cv.ShowImage(filename, bgr)
cv.WaitKey()
I believe the OpenCV matrix type implements the python interfaces for memoryview et al – don’t be scared off by those objects as they’re NumPy arrays with different socks on, if you will.
†) quickly, w/r/t both the developer sense of speed: you can use this stuff immediately instead of building SciPy addons or mucking about with the Python array view C interface; but also in the real sense, as everything that comprises the aforementioned stuff – the OpenCV matrix structs, their related Python C API underpinnings, the OpenEXR format, and the stock implementation of the interface to same – have been raked over the optimization coals for years, largely by notable and grant-backed squadrons of specialist scholar-nerds who know what they are doing in this arena.

Different results with Haar cascade when in release/debug mode

I am using a Haar cascade classifier trained from the MIT cars dataset to detect vehicles in OpenCV (trained using the utilities provided with OpenCV). This works reasonably well when compiled in Debug mode, but when compiled in Release mode the cascade doesn't make any detections at all. Running the following code on the test image below gives a detection in debug mode but nothing in release mode (this behaviour continues through all images in my data sequence).
Can you suggest why this occurs and, more importantly, what I can do to obtain detections when running in Release mode?
Cascade File
Code
cv::Mat testImage = cv::imread("testImage.png",0);
cv::equalizeHist(testImage, testImage);
cv::CascadeClassifier vehicleCascade;
vehicleCascade.load("cars3.xml");
// Detect vehicles
std::vector<cv::Rect> cars;
vehicleCascade.detectMultiScale(
testImage, // Input image
cars, // Output bounding boxes
1.1, // scale factor - how much image size is reduced at each scale
5, // min neighbours - how many neighbours required to maintain rect
0|CV_HAAR_SCALE_IMAGE, // Not used
cv::Size(30,30), // Min poss object size
cv::Size() // Max poss object size
);
std::cout << "Found " << cars.size() << " objects.\n";
for (int i=0; i<cars.size(); ++i)
cv::rectangle(testImage, cars.at(i), CV_RGB(255,0,0), 3);
cv::namedWindow("Haar cascade");
cv::imshow("Haar cascade", testImage);
cv::waitKey(0);
cv::imwrite("output.png", testImage);
TestImage
It's strange, but for me you code is working fine in both modes. I'm using Visual Studio 2010 on Windows 7 32bit. Here is my project - https://www.dropbox.com/s/5kubn5tlu7k6ziy/opencvhw.rar, so you can check executables(Release and Debug directories). If you are using visual studio and want to build it on your own you will have to change paths to library and include directories for both modes(include directories path is the same, library directories path is different). (project -> ... properties -> configuration properties -> vc++ directories)
Generally i would recommend to check really carefully project configuration. It's really easy to make small mistake which can cause very weird behavior. Sometimes the best option is to configure everything from the scratch once again.
OpenCV have very strange bugs - it's nothing new :) For example i can't use some codecs while debugging - if i run program in debug mode, but without debugging everything is fine, but if i try to debug - each frame readed from file is empty(but its size is correct).
It's possible that you have just encountered something a bit similar. Try to convert image to different format(i think that bmp is the best choice for test - it's should always work without any additional libraries).
Also note that you have uploaded jpg file so i had to rename and convert it on my own - maybe during conversions something in this file have changed so we are not testing your code on exactly the same files - upload you png file on dropbox so i can test it.
See this post:
This type of error could be caused by linking to the debug library of opencv in release mode.