CIDetector.RectDetector bounds to view bounds coordinates - ios

So,
I am trying to display a rectanlge around a detected document (A4)
I am using an AVCaptureSession for the feed along with the AVCaptureStillImageOutput Output.
NSError Error = null;
Session = new AVCaptureSession();
AVCaptureDevice Device = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video);
AVCaptureDeviceInput DeviceInput = AVCaptureDeviceInput.FromDevice(Device, out Error);
Session.AddInput(DeviceInput);
AVCaptureStillImageOutput CaptureOutput = new AVCaptureStillImageOutput();
CaptureOutput.OutputSettings = new NSDictionary(AVVideo.CodecKey, AVVideo.CodecJPEG) ;
Session.AddOutput(CaptureOutput);
I have a timer that takes the output and passes that to my handler
NSTimer.CreateRepeatingScheduledTimer(TimeSpan.Parse("00:00:02"), delegate
{
CaptureImageWithMetadata(CaptureOutput,CaptureOutput.Connections[0]);
});
I also have an AVCapturePreviewLayer with its bound being full screen (iPad Mini Portrait)
PreviewLayer = new AVCaptureVideoPreviewLayer(Session);
PreviewLayer.Frame = this.View.Frame;
PreviewLayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;
this.View.Layer.AddSublayer(PreviewLayer);
PreviewLayer.ZPosition = (PreviewLayer.ZPosition - 1);
Below is the handler
private async void CaptureImageWithMetadata(AVCaptureStillImageOutput output, AVCaptureConnection connection)
{
var sampleBuffer = await output.CaptureStillImageTaskAsync(connection);
var imageData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer);
var image = CIImage.FromData(imageData);
var metadata = image.Properties.Dictionary.MutableCopy() as NSMutableDictionary;
CIContext CT = CIContext.FromOptions(null);
CIDetectorOptions OP = new CIDetectorOptions();
OP.Accuracy = FaceDetectorAccuracy.High;
OP.AspectRatio = 1.41f;
CIDetector CI = CIDetector.CreateRectangleDetector(CT, OP);
CIFeature[] HH = CI.FeaturesInImage(image,CIImageOrientation.BottomRight);
CGAffineTransform Transfer = CGAffineTransform.MakeScale(1, -1);
Transfer = CGAffineTransform.Translate(Transfer, 0, -this.View.Bounds.Size.Height);
if (HH.Length > 0)
{
CGRect RECT = CGAffineTransform.CGRectApplyAffineTransform(HH[0].Bounds, Transfer);
Console.WriteLine("start");
Console.WriteLine("IMAGE : "+HH[0].Bounds.ToString());
Console.WriteLine("SCREEN :"+RECT.ToString());
Console.WriteLine("end");
BB.Frame = RECT;
BB.Hidden = false;
}
}
Despite however after following a guid that suggested I need to convert the coordinates - my highlighter (green) is not surround the document, and i cant figure out why.
I am using CIImageOrientation.BottomRight just as test but no matter what i put here.. always the same result. See Images

Related

Nativescript BitmapFactory resize does not work in iOS

I am using the BitmapFactory plugin to rezize pictures to save them as thumnails
let mutable = BitmapFactory.makeMutable(imageSource);
let rotatedBitmap = BitmapFactory.asBitmap(mutable).dispose((bmp) =>
{
return bmp.resize("80,80");
});
in Android it works perfectly, but in iOS the image returned is not resized. I read in some issues that this is because the iOS uses the default scaling..?
does anybody knnow how to resize images to square thumbnaile with a given size ?
I use undermentioned function...
imageSource = imageSourceModule.fromFile(filepath);
try
{
var mutable = BitmapFactory.makeMutable(imageSource);
var ThumbBitmap = BitmapFactory.asBitmap(mutable).dispose((bmp) =>
{
var optisizestring = "25,25";
test = bmp.resize(optisizestring);
base64JPEG = test.toBase64(BitmapFactory.OutputFormat.JPEG, 75);
img = imageSource.fromBase64(base64JPEG);
resolve( "data:image/png;base64," + base64JPEG);
global.gc(); // tried with \ without garbage collection here
});
} catch(ex) { console.log("errds " + ex); resolve (null);}
(in the BitmapFactory-plugin) BitmapFactory.ios.js i changed the resize function
iOSImage.prototype._resize = function(newSize) {
var oldImg = this._nativeObject;
try {
var ns = CGSizeMake(newSize.width, newSize.height);
UIGraphicsBeginImageContext(ns);
var context = UIGraphicsGetCurrentContext();
oldImg.drawInRect(CGRectMake(0, 0,ns.width, ns.height));
return UIGraphicsGetImageFromCurrentImageContext();
}
finally
{
UIGraphicsEndImageContext();
}
};
// // [INTERNAL] _resize() Original
// iOSImage.prototype._resize = function(newSize) {
// var oldImg = this._nativeObject;
// try {
// var ns = CGSizeMake(newSize.width, newSize.height);
// UIGraphicsBeginImageContextWithOptions(ns, false, 0.0);
// oldImg.drawInRect(CGRectMake(0, 0,
// ns.width, ns.height));
// return new iOSImage(UIGraphicsGetImageFromCurrentImageContext());
// }
// finally {
// UIGraphicsEndImageContext();
// }
// };

Is there a way get original foreground object colors foreground substraction?

I am using BackgroundSubtractorMOG2() to extract foreground of video.I am wondering about "how to get original colors of the image and set it foreground".I want to extract objects with original colors not threshold.
Foreground Extraction Code:
let video = document.getElementById('videoInput');
let cap = new cv.VideoCapture(video);
let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
let fgbg = new cv.BackgroundSubtractorMOG2(500, 16, true);
const FPS = 30;
function processVideo() {
try {
if (!streaming) {
// clean and stop.
frame.delete(); fgmask.delete(); fgbg.delete();
return;
}
let begin = Date.now();
// start processing.
cap.read(frame);
fgbg.apply(frame, fgmask);
cv.imshow('canvasOutput', fgmask);
// schedule the next one.
let delay = 1000/FPS - (Date.now() - begin);
setTimeout(processVideo, delay);
} catch (err) {
utils.printError(err);
}
};
// schedule the first one.
setTimeout(processVideo, 0);
Original image
My output
I want this

How to perform affine transform on stream?

I want to rotate image, which I get from memoryStream.
I've tried to use UIView:
private Stream TransformImageFromStream(MemoryStream stream, CGAffineTransform transform)
{
var bytes = stream.ToBytes ();
var data = NSData.FromArray (bytes);
var image = UIImage.LoadFromData (data);
var uiImage = new UIImageView (image);
uiImage.Transform = transform;
var result = uiImage.Image.AsPNG ().AsStream ();
var testBytes = result.ToBytes ();
if (testBytes [0] == bytes [0])
{
// throw new Exception ("test failed");
}
return result;
}
But transform never applies as it always do with UIView within graphic canvas.
I've found somewhere that it will work with using ApplyTransform directly to CIImage
private Stream TransformImageFromStream(MemoryStream stream, CGAffineTransform transform)
{
var bytes = stream.ToBytes ();
using (var data = NSData.FromArray (bytes))
using (var ciImage = CIImage.FromData (data))
using (var transformedImage = ciImage.ImageByApplyingTransform (transform))
using (var uiImage = UIImage.FromImage(ciImage))
using (var uiImageView = new UIImageView(uiImage))
{
var result = uiImage.AsPNG ();
return null;
}
}
But UIImage doesnt want to convert it AsPng() in order to convert it to stream. In this case I've noticed that CGImage is empty, most of properties set to 0. Perhaps, there is some way to convert the CIImage itself, without any wrapping?
I have no more clues to what to do.

How to write to file (encode) modified camera frames in Windows Phone 8

I have custom an camera app in Windows Phone 8. I need add a watermark image to each frame from camera capture and then record to a video.
I can customise each frame from the preview using the following code:
int[] pixelData = new int[(int)(camera.PreviewResolution.Width * camera.PreviewResolution.Height)];
camera.GetPreviewBufferArgb32(pixelData);
return pixelData;
and write it back to the preview.
my problem is that while I can show the frames on the screen while the user is recording a movie using the camera I cant find a working solution for WP8 to encode the frames and audio to save to a file.
I already tried opencv,libav and others without success,
if anyone can point me to the right direction it would be greatly appreciated.
You can do it like this.
private void GetCameraPicture_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.CameraCaptureTask cameraCaptureTask = new Microsoft.Phone.Tasks.CameraCaptureTask();
cameraCaptureTask.Completed += cct_Completed;
cameraCaptureTask.Show();
}
try
{
if (e.TaskResult == Microsoft.Phone.Tasks.TaskResult.OK)
{
var imageStream = e.ChosenPhoto;
var name = e.OriginalFileName;
using (MemoryStream mem = new MemoryStream())
{
TextBlock tb = new TextBlock() { Text = DateTime.Now.ToString("dd MMM yyyy, HH:mm"), Foreground = new SolidColorBrush(Color.FromArgb(128, 0, 0, 0)), FontSize = 40 };
BitmapImage finalImage = new BitmapImage();
finalImage.SetSource(imageStream);
WriteableBitmap wbFinal = new WriteableBitmap(finalImage);
wbFinal.Render(tb, null);
wbFinal.Invalidate();
wbFinal.SaveJpeg(mem, wbFinal.PixelWidth, wbFinal.PixelHeight, 0, 100);
mem.Seek(0, System.IO.SeekOrigin.Begin);
MediaLibrary lib = new MediaLibrary();
lib.SavePictureToCameraRoll("Copy" + name, mem.ToArray());
}
}
}
catch (Exception exp) { MessageBox.Show(exp.Message); }
Hope it may help you.

AS2 Print External image

I have this script where I can print the movie that i indicated in the code.
function print_page()
{
var my_pj = new PrintJob();
var myResult = my_pj.start();
if (myResult)
{
myResult = my_pj.addPage("img_mc", null, {printAsBitmap:true}, 1);
my_pj.send();
delete my_pj;
trace("ok");
}
}
I need to know, how to print some image that is outside the flahs... clicking in the MC.... but i need in AS2...
ty for help!
This is the only way i sucess print external image with PrintJob() and AS2
#include "mc_tween2.as"
_root.myholder.loadMovie("teste_labirinto.jpg");
_root.myholder._x = 0; //i set the x and y position of image out of the screen area!
_root.myholder._y = 990; //
function print_page()
{
var my_pj = new PrintJob();
var myResult = my_pj.start();
if (myResult)
{
my_pj.orientation = portrait;
myResult = my_pj.addPage("myholder", {xMin:0, xMax:600, yMin:0, yMax:800}, {printAsBitmap:true}, 1);
my_pj.my_pj.send();
delete my_pj;
trace("ok");
}
}
btn.onRelease = function()
{
print_page();
};

Resources