I am trying to make a gaussian blur with opencv in ios. I have already included opencv into my app, but I don't know how to call it so that it makes a gaussian blur.
In Android I call it like this:
Mat source = Highgui.imread(filepath, Highgui.CV_LOAD_IMAGE_COLOR);
Mat destination = new Mat(source.rows(),source.cols(),source.type());
Imgproc.GaussianBlur(source, destination, new Size(45, 45), 0);
Highgui.imwrite(getDir().getPath() + File.separator +"Gaussian45.jpg", destination);
Is there something similar in iOS too?
Thanks for your answers.
By the looks of things, You have to convert it to a cv::Mat, then you can use the normal guassian blur c++ method and then convert it back to ULLImage
The above link demonstrates how to convert from and to the two image types. Once you have converted it to cv::Mat you simply use this method:
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int
Related
What I'm curious is simple.
We created video calling functionality using mobile technology on WebRTC.
In addition, I would like to use the OpenCV library to add face detection during video calls.
To implement this function, it is necessary to convert Bitmap to WebRTC I420Frame.
Is there a way?
Using libYuv (https://chromium.googlesource.com/libyuv/libyuv/) would likely be the best way. Once you determine what type the bitmap is (rgb, bgr, argb, etc...), you can pass it to libYuv and it will convert it to i420 for you in an efficient way.
You could also run the calculations yourself if you really wanted to but you wouldn't get the arm specific performance calls that libYuv would do for you.
Using libYuv https://chromium.googlesource.com/libyuv/libyuv/
The following code works:
size_t file_size = frame.width() * frame.height() * 3;
std::unique_ptr<uint8_t[]> res_rgb_buffer2(new uint8_t[file_size]);
webrtc::ConvertFromI420(frame, webrtc::VideoType::kRGB24, 0,
res_rgb_buffer2.get());
now res_rgb_buffer2 contains a pointer for raw BMP data. you can save it in a file but you need to write file header first. the latter part can be found here.
I want to add Pie chart in my PDF using itext in Android.
But all the example related to piechart seems to use the following 2D classes:
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
that are actually awt classes and not available in android version of itext-5.
Any alternate to using 2D classses ?
Edited: I am using itextg version for android. Its missing 2D classes mentioned above.
So i have found a solution.
I was using MPAndroidChart for making the PieChart.
This library has a method for getting the bitmap from the drawn Piechart.
pieChart.getChartBitmap();
So, once you get the bitmap, iText will let you add the bitmap into your pdf.
The final code may looks like this:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = pieChart.getChartBitmap();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
document.add(myImg);
Cheers!
For Android you need to use itextg instead of itextpdf.
Examples that use classes that are not available on Android, should not be used. When you find a solution, it would be nice if you submitted your documentation.
I am trying to load images into a Mat in openCV for Android for face recognition.
The images are in in jpeg format of size 640 x 480.
I am using Eclipse and this codes are in .cpp file.
This is my codes.
while (getline(file, line)) {
stringstream liness(line);
getline(liness, path, ',');
getline(liness, classlabel);
if(!path.empty() && !classlabel.empty()) {
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
However, I am getting an error saying that "The matrix is not continuous, thus its number of rows cannot be changed in function cv::Mat cv:Mat:reshape(int,int)const"
I tried using the solution in OpenCV 2.0 C++ API using imshow: returns unhandled exception and "bad-flag"
but it's in Visual Studio.
Any help would be greatly appreciated.
Conversion of image from Camera preview.
The image is converted to Grayscale from camera preview data.
Mat matRgb = new Mat();
Imgproc.cvtColor(matYuv, matRgb, Imgproc.COLOR_YUV420sp2RGB, 4);
try{
Mat matGray = new Mat();
Imgproc.cvtColor(matRgb, matGray, Imgproc.COLOR_RGB2GRAY, 0);
resultBitmap = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(matGray, resultBitmap);
Saving image.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmFace[0].compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] flippedImageByteArray = stream.toByteArray();
the 'Mat not continuous' error is not at all related to the link you have there.
if you're trying fisher or eigenfaces, the images have to get 'flattened' to a single row for the pca.
this is not possible, if the data has 'gaps' or was padded to make the row size a multiple of 4. some image editors do that to your data.
also, imho your images are by far too large ( pca works best, when it'S almost quadratic, ie the rowsize (num_pixels) is similar to the colsize(num_images).
so my proposal would be, to resize the train images ( and also the test images later ) to something like 100x100, when loading them, this will also achieve a continuous data block.
(and again, avoid jpegs for anything image-processing related, too many compression artefacts!)
This is my code. I am using FFmpegFrameGrabber to grab frames one by one from a video stored in my android file system. I need some way to convert a IplImage or a Frame to a Mat so that I can perform some functions on it.
FFmpegFrameGrabber grabber=new FFmpegFrameGrabber("/data/data/some.path.name/files/images/1.mp4");
grabber.start();
while(true){
IplImage image=grabber.grab();
// or Frame image=grabber.grabFrame();
/*
some code to convert Frame/IplImage to Mat.
*/
}
grabber.stop();
grabber.release();
In C++ cv::Mat has a constructor that takes an IplImage
C++: Mat::Mat(const IplImage* img, bool copyData=false)
There should also be one in your JavaCV.
On the other hand you should be able to use your IplImage directly with OpenCV without the need to convert to cv::Mat.
EDIT: Have a look at this Question on SO: Convert IplImage to Mat in javacv
I have an array of bytes that correspond to a "grayscaled bitmap" (one byte->one pixel), and I need to create a PNG file for this image.
The method below works, but the png created is HUGE, as the Bitmap I am using is an ARGB_8888 bitmap, which takes 4 bytes per pixel instead of 1 byte.
I haven't been able to make it work with other Bitmap.Config different than ARGB_8888. Maybe ALPHA_8 is what I need, but I have not been able to make it work.
I have also tried the toGrayScale method which is included in some other posts (Convert a Bitmap to GrayScale in Android), but I have the same issue with the size.
public static boolean createPNGFromGrayScaledBytes(ByteBuffer grayBytes, int width,
int height,File pngFile) throws IOException{
if (grayBytes.remaining()!=width*height){
Logger.error(Tag, "Unexpected error: size mismatch [remaining:"+grayBytes.remaining()+"][width:"+width+"][height:"+height+"]", null);
return false;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// for each byte, I set it in three color channels.
int gray,color;
int x=0,y=0;
while(grayBytes.remaining()>0){
gray = grayBytes.get();
// integer may be negative as byte is signed. make them positive.
if (gray<0){gray+=256;}
// for each byte, I set it in three color channels.
color= Color.argb(-1, gray, gray, gray);
bitmap.setPixel(x, y, color);
x++;
if (x==width){
x=0;
y++;
}
}
FileOutputStream fos=null;
fos = new FileOutputStream(pngFile);
boolean result= bitmap.compress(Bitmap.CompressFormat.PNG,100,fos);
fos.close();
return result;
}
EDIT: Link to the generated file (it may look nonsense, but is just created with randon data).
http://www.tempfiles.net/download/201208/256402/huge_png.html
Any help will be greatly appreciated.
As you've noticed, saving a grayscale image as RGB is expensive. If you have luminance data then it would be better to save as a Grayscale PNG rather than an RGB PNG.
The bitmap and image functionality available in the Android Framework is really geared towards reading and writing image formats that are supported by the framework and UI components. Grayscale PNG is not included here.
If you want to save out a Grayscale PNG on Android then you'll need to use a library like http://code.google.com/p/pngj/
If you use OPENCV for Android library, you can use the library to save a binary data to a png file.
My way is:
in jni part,
set Mat whose data begin with the byte array:
jbyte* _ByteArray_BrightnessImgForOCR = env->GetByteArrayElements(ByteArray_BrightnessImgForOCR, 0);
Mat img(ByteArray_BrightnessImgForOCR_h, ByteArray_BrightnessImgForOCR_w, CV_8UC1, (unsigned char *) _ByteArray_BrightnessImgForOCR);
And then write it to a png file.
imwrite("/mnt/sdcard/binaryImg_forOCR.png", img);
Of course, you need to take some time to get yourself familiar with OpenCV and Java native coding. Following OpenCV for Android examples, it is fast to learn.