This week I had to deal with OpenCV. An old open-source library from Intel which deals with image processing. The "old" means it must be wrapped for usage inside C#, I've found SharperCV does the job and skipped the writing of C++ wrapper.
Next step is compare 2 images.
Welp, it appears that comparing 2 images is not as trivial as you might think, first thing to do is make sure they have the same scale (obvious, but not ensured.) This barrier is non-existent when comparing the image to itself.
After seeing results from the "trivial" comparison I moved on.
The second thing to do is define the comparison method, in the case I was responsible for we chose the easy ones: Xor, optical flow.
Xor is a very good indication as to whether the images are the same or not, low (enough) Xor.sum means the images are identical (too the lowness degree.) Yet even the static frame of "error" has a value larger than 0 for its Xor.
The code for the Xorring:
SharperCV.CvImage CurImage = new SharperCV.CvImage("input1.jpg");
SharperCV.CvImage PrevImage = new SharperCV.CvImage("input2.jpg");
SharperCV.CvImage Xorred = CurImage.Xor(PrevImage, null);
double XorredAlpha = Xorred.Sum().val0;
Optical flow, on the other hand returns a good indication to whether there's a change, but a black frame turning to white frame results in "no change".
A most important thing to do is turn the images to grey, otherwise a nice error message appears.
And the code is:
SharperCV.CvImage greyCurImg = new SharperCV.CvImage(CurImage.Size, SharperCV.BitDepths.IPL_DEPTH_8U, 1);
SharperCV.CvImage greyPrevImg = new SharperCV.CvImage(PrevImage.Size, SharperCV.BitDepths.IPL_DEPTH_8U, 1);
SharperCV.CvImage BMvelx = null;
SharperCV.CvImage BMvely = null;
greyCurImg.CalcOpticalFlowBM(greyPrevImg, blockSize, shiftSize, maxRange, out BMvelx, out BMvely);
I've used the following definitions, but that's cause "it works".
SharperCV.CvSize blockSize = new SharperCV.CvSize(16, 16);
SharperCV.CvSize shiftSize = new SharperCV.CvSize(4, 4);
SharperCV.CvSize maxRange = new SharperCV.CvSize(16, 16);
Be careful, the resulting images are small (we did some "blocking"), but the real value lies in the average, which gives the indication of "change".
Of course fine tuning is required for the results, but that's a must in almost every statistical system.
Thursday, October 25, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment