Ask Your Question

redleon80's profile - activity

2019-01-29 10:17:03 -0600 received badge  Famous Question (source)
2017-11-25 07:24:38 -0600 received badge  Notable Question (source)
2017-06-07 14:12:08 -0600 received badge  Popular Question (source)
2016-12-15 14:56:25 -0600 commented question How to Increase JPG file size?

I tried to the all jpeg params: "JPEG_CHROMA_QUALITY, IMWRITE_JPEG_LUMA_QUALITY, IMWRITE_JPEG_OPTIMIZE, IMWRITE_JPEG_PROGRESSIVE, IMWRITE_JPEG_QUALITY, IMWRITE_JPEG_RST_INTERVAL"

2016-12-15 01:45:53 -0600 commented question How to Increase JPG file size?

My app generate jpeg for special web application. The web application ask static resolution and at least 20KB file size. If jpeg file smaller than 20KB you cant upload the jpeg file. For the reason I need to generate fixed resolution and bigger than 20KB on earth.

2016-12-15 00:00:19 -0600 commented question How to Increase JPG file size?

@Eduardo I now JPEG compressed image. But I need to have a little bit bigger jpeg file size. I edited the jpeg file in MSPaint, Photoshop etc. MSPaint save it smallest, but Photoshop can save bigger size if I chouse "add color profile" option.

I added exifdatas but this's not enough.

2016-12-14 23:49:48 -0600 commented question How to Increase JPG file size?

Yes you right @LBerger. The question maybe is not all about opencv. But opencv libraries can make manipulation on image. I dont know maybe I can

  • add text jpeg files
  • add color profile data
  • add noise

to increase file. I added opencv library and all works fine. Just I need an example.

2016-12-14 12:30:27 -0600 asked a question How to Increase JPG file size?

I'm getting byte image data from native camera, takePicture and pictureCallBack. And using opencv libraries for rotating, croping and resising. I'm using Bitmap.ComptressFormat and FileExportStream to save image. Everything work fine. But It have to create jpg file size grather than 20KB but sometimes it's getting smaller than 20KB. I also add exif datas but it's doest't give target file size values.

Is there any way to increase file size? I tried imwrite but it give smallest results. I couldn't success to find save jpg without compression.

PictureCallback myPictureCallback_JPG = new PictureCallback(){
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getJpgFile();

       data = cropImage2(data);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();

            addEXIFData(pictureFile);

        } catch (FileNotFoundException e) {
            Log.d("Method.PictureCallBack", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Method.PictureCallBack", "Error accessing file: " + e.getMessage());
        }

public byte[] cropImage (byte[] data) {
        Mat lastMat = new Mat();

        Mat mat1_img = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

        Mat mat2_rotated = new Mat();

        Core.flip(mat1_img.t(), mat2_rotated, 1);

        Mat mat3_cropped = new Mat(mat2_rotated, cropRect);

        Imgproc.resize(mat3_cropped, lastMat, cropSize);

        Imgproc.cvtColor(lastMat, lastMat, Imgproc.COLOR_BGR2RGB,1);
        Bitmap bitmapImg = Bitmap.createBitmap(lastMat.cols(), lastMat.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(lastMat, bitmapImg);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bos);

        return bos.toByteArray();
    }
2016-06-02 11:10:15 -0600 received badge  Nice Question (source)
2016-05-30 15:35:34 -0600 asked a question How to return and show only cropped area on CameraView?

I cand cropped CameraFrame. When I return the cropped area then I'm getting error.

 public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

        Mat imgSrc = inputFrame.rgba;

        Point fPoint = new Point(0, imgSrc.rows()/6);
        Point lPoint = new Point(imgSrc.cols(), imgSrc.rows()*5/6);
        Rect rect = new Rect(fPoint, lPoint);

        Imgproc.rectangle(imgSrc, fPoint, lPoint, new Scalar(0, 255, 0));

        Mat croppedImage = new Mat(imgSrc, rect);

        return croppedImage;
    }

And getting this error:

E/cv::error(): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
E/CameraBridge: Mat type: Mat [ 320*864*CV_8UC4, isCont=true, isSubmat=true, nativeObj=0x5f773ff8, dataAddr=0x60cb4810 ]
E/CameraBridge: Bitmap type: 864*480
E/CameraBridge: Utils.matToBitmap() throws an exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
2016-05-27 18:30:05 -0600 received badge  Self-Learner (source)
2016-05-27 17:50:21 -0600 answered a question How to detect marked black regions inside largest Rectangle Contour?

I imrove @sturkmen' s code.

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{your package name}.FragmentMain">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Test" />

    <ImageView
        android:id="@+id/sampleImageView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"/>
</LinearLayout>

</framelayout>

AndroidManifest.xml

Add this line for write permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

FragmentMain.java

IMAGE FILE: Add Internal Storage / Android / Data / Your Package Folder / test.JPG

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View _view = inflater.inflate(R.layout.fragment_main, container, false);

    Button btnTest = (Button) _view.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Mat img = Imgcodecs.imread(mediaStorageDir().getPath() + "/" + "test.JPG");
            if (img.empty()) {
                Log.d("FragmentMain", "Empty Image");
            }


            Size dims = new Size (20,5);
            Mat gray = new Mat();
            Mat thresh = new Mat();

            //convert the image to black and white
            Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
            storeImage(gray);

            //convert the image to black and white does (8 bit)
            Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);

            storeImage(thresh);

            Mat temp = thresh.clone();
            //find the contours
            Mat hierarchy = new Mat();

            Mat corners = new Mat(4,1, CvType.CV_32FC2);
            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            hierarchy.release();

            for (int idx = 0; idx < contours.size(); idx++)
            {
                MatOfPoint contour = contours.get(idx);
                MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
                RotatedRect minRect = Imgproc.minAreaRect( contour_points );
                Point[] rect_points = new Point[4];
                minRect.points( rect_points );
                if(minRect.size.height > img.width() / 2)
                {
                    List<Point> srcPoints = new ArrayList<Point>(4);
                    srcPoints.add(rect_points[2]);
                    srcPoints.add(rect_points[3]);
                    srcPoints.add(rect_points[0]);
                    srcPoints.add(rect_points[1]);

                    corners = Converters.vector_Point_to_Mat(
                            srcPoints, CvType.CV_32F);
                }

            }
            Imgproc.erode(thresh, thresh, new Mat(), new Point(-1,-1), 10);

            storeImage(thresh);
            Imgproc.dilate(thresh, thresh, new Mat(), new Point(-1,-1), 5);

            storeImage(thresh);

            Mat results = new Mat(1000,250,CvType.CV_8UC3);
            Mat quad = new Mat(1000,250,CvType.CV_8UC1);

            List<Point> dstPoints = new ArrayList<Point>(4);
            dstPoints.add(new Point(0, 0));
            dstPoints.add(new Point(1000, 0));
            dstPoints.add(new Point(1000, 250));
            dstPoints.add(new Point(0, 250));
            Mat quad_pts = Converters.vector_Point_to_Mat(
                    dstPoints, CvType.CV_32F);

            Mat transmtx = Imgproc.getPerspectiveTransform(corners, quad_pts);
            Imgproc.warpPerspective( img, results, transmtx, new Size(1000,250));
            Imgproc.warpPerspective( thresh, quad, transmtx, new Size(1000,250));

            Imgproc.resize(quad, quad, new Size(20,5));

            Imgcodecs.imwrite("results.png",quad);

            //store image
            storeImage(quad);

            //show image
            showImage(quad);


            System.out.println( quad.dump() );

            for(int i = 0; i < quad.cols(); i++)
            {
                int size = (int) (quad.total() * quad.channels());
                byte[] tmp = new byte[size];

                String answer = "";
                double[] d = new double[0];
                d = quad.get(1, i);
                answer += d[0] == 0 ? "" : "A";
                d = quad.get(2, i);
                answer += d[0] == 0 ? "" : "B";
                d = quad.get(3, i);
                answer ...
(more)
2016-05-27 16:40:32 -0600 commented answer How to detect marked black regions inside largest Rectangle Contour?

@sturkmen I totally understand the algorytim with your code. This is so helpful. It's work on Android now. Thank you so much

2016-05-27 04:24:12 -0600 received badge  Student (source)
2016-05-26 04:36:10 -0600 commented answer How to detect marked black regions inside largest Rectangle Contour?

Tried your Java code on Android. And getting your result img now. You used Mat.at<uchar> method in your c++ code. Is there any Mat method in java like this? how can I get results by text? A,B,C,D or empty column?

2016-05-26 04:27:40 -0600 received badge  Enthusiast
2016-05-23 14:45:31 -0600 commented answer How to detect marked black regions inside largest Rectangle Contour?

thank you very much. I'm going to try as soon as possible. @sturkmen

2016-05-23 14:45:06 -0600 commented question How to detect marked black regions inside largest Rectangle Contour?

Thank you @LBerger. Your list about methods I guess. Firstly I need to figure it out.

2016-05-23 14:43:31 -0600 received badge  Supporter (source)
2016-05-22 02:35:32 -0600 asked a question How to detect marked black regions inside largest Rectangle Contour?

image description

I can detect largest contour the answer sheet (20 questions, each have 4 alternative)

After the draw largest contour, what shall I do? Divide matris the rectangle by 20x4 cell? Or find countour again but this time inside the rectangle? I dont know what I need. Just I want to get which is marked.

 public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

        return findLargestRectangle(inputFrame.rgba());
    }


    private Mat findLargestRectangle(Mat original_image) {
        Mat imgSource = original_image;
        hierarchy = new Mat();

        //convert the image to black and white
        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

        //convert the image to black and white does (8 bit)
        Imgproc.Canny(imgSource, imgSource, 50, 50);

        //apply gaussian blur to smoothen lines of dots
        Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);

        //find the contours
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(imgSource, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        hierarchy.release();

        double maxArea = -1;
        int maxAreaIdx = -1;
        MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        Mat largest_contour = contours.get(0);
        List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
        for (int idx = 0; idx < contours.size(); idx++) {
            temp_contour = contours.get(idx);
            double contourarea = Imgproc.contourArea(temp_contour);
            //compare this contour to the previous largest contour found
            if (contourarea > maxArea) {
                //check if this contour is a square
                MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
                int contourSize = (int)temp_contour.total();
                Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
                if (approxCurve.total() == 4) {
                    maxArea = contourarea;
                    maxAreaIdx = idx;
                    largest_contours.add(temp_contour);
                    largest_contour = temp_contour;
                }
            }
        }
        MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
        largest_contours = new ArrayList<MatOfPoint>();
        largest_contours.add(temp_largest);


        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
        Imgproc.drawContours(imgSource, contours, maxAreaIdx, new Scalar(0, 255, 0), 1);
        Log.d(TAG, "Largers Contour:" + contours.get(maxAreaIdx).toString());


        return imgSource;
    }

UPDATE 1: I want to thank you @sturkmen for the his answer. I can read and find black regions now. Here the Android codes:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View _view = inflater.inflate(R.layout.fragment_main, container, false);
    // Inflate the layout for this fragment


    Button btnTest = (Button) _view.findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Mat img = Imgcodecs.imread(mediaStorageDir().getPath() + "/" + "test2.jpg");
            if (img.empty()) {
                Log.d("Fragment", "IMG EMPTY");
            }


            Mat gray = new Mat();
            Mat thresh = new Mat();

            //convert the image to black and white
            Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);

            //convert the image to black and white does (8 bit)
            Imgproc.threshold(gray, thresh, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
            Mat temp = thresh.clone();
            //find the contours
            Mat hierarchy = new Mat();

            Mat corners = new Mat(4,1, CvType.CV_32FC2);
            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(temp, contours,hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            hierarchy.release();

            for (int idx = 0; idx < contours.size(); idx++)
            {
                MatOfPoint contour = contours.get(idx);
                MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());
                RotatedRect minRect = Imgproc.minAreaRect( contour_points );
                Point[] rect_points = new Point[4];
                minRect.points( rect_points );
                if(minRect.size.height > img.width() / 2)
                {
                    List<Point> srcPoints = new ArrayList<Point>(4);
                    srcPoints.add(rect_points[2]);
                    srcPoints.add(rect_points ...
(more)