putText not working for video frame

asked 2018-10-27 12:59:58 -0600

jayaise gravatar image
 Mat m = new Mat (720, 1280, CvType.CV_8UC4);
m.put(0,0,vf.rawData.getBytes());
Imgproc.putText(m, "Welcomeeeeeeeeeeeeeee", new org.opencv.core.Point(400,400),
  Core.FONT_HERSHEY_PLAIN, 1.0 ,new Scalar(255,0,0));
//byte[] buffer = new byte[((int)m.total())*m.channels()];
byte[] buffer = new byte[vf.rawData.getBytes().length];
m.get(0,0,buffer);
FileOutputStream fos = new FileOutputStream("C:/Users/h130954/images/Image-" + BAbsTime.now().getTimeOfDayMillis() +".jpg");
        fos.write(buffer);
        fos.close();
edit retag flag offensive close merge delete

Comments

I am trying to insert text welcome into frame, but its not working. The image does not contain the text I am trying to add

jayaise gravatar imagejayaise ( 2018-10-27 13:00:49 -0600 )edit
1

I'm not very experienced with Java, but nothing there does any encoding into the JPEG format, does it? If you have a JPEG image, you can't just put it into a Mat. You have to decode it first. If you have a Mat, you can't just stuff the bytes into a JPEG file and expect to see anything, you have to encode it first.

Tetragramm gravatar imageTetragramm ( 2018-10-27 20:33:23 -0600 )edit

The frame is from live video and It’s jpeg only, I am converting frame to mat and trying to add text and then converting back to bytes. How to encode and decode, how to do this in OpenCV

jayaise gravatar imagejayaise ( 2018-10-27 22:02:14 -0600 )edit

btw, if the input was from a video, it's probably CV_8UC3, not CV_8UC4

and i somehow suspect, you don't need that byte[] buffer at all. you could simply use imwrite(), to save your Mat to disc.

berak gravatar imageberak ( 2018-10-28 07:10:55 -0600 )edit

CV_8UC3 did not work for me, CV_8UC4 works for me. Below are the steps I am doing: 1>Taking frame from video, which is in bytes 2> creating mat object for it 3> Modifying Mat Object and inserting text using putText 4> then again converting mat object back to bytes.(decoding frames) 5> This bytes is displayed video.

Do I need to do Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY); to insert text???

jayaise gravatar imagejayaise ( 2018-10-28 10:27:44 -0600 )edit

Also this video is from Axis IP camera

jayaise gravatar imagejayaise ( 2018-10-28 10:36:15 -0600 )edit

Do I need to encode/decode??

jayaise gravatar imagejayaise ( 2018-10-28 11:17:20 -0600 )edit
  1. why not use the VideoCapture class to read the ip stream ?
  2. putText() on the mat you get from there
  3. imwrite() that mat to disc.
    again no, you don't need any byte[] buffers.
berak gravatar imageberak ( 2018-10-28 11:46:17 -0600 )edit