Android Question Getting Pixel Co-ordinates from Camera Image

ChocoScope

Member
Licensed User
Longtime User
I would like to generate a set of pixel co-ordinates from a camera image which are lit up RED and save it in a binary file. I want to generate it from the image in memory rather than grabbing a picture and storing it as .jpg as I want to do it multiple times. The final file will be used for data (pictures) from a 3D laser scanner. Can anyone please point me to or supply suitable programs to do this.
 

JordiCP

Expert
Licensed User
Longtime User
I was hoping that you just copied the new code, as it isn't an improvement but an error fix

I had forgotten this line, which is a Sub declaration, just after Sub button1_Click
B4X:
Sub  Dotty( PreviewPic() as Byte )
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
That line was there before on the original #16 post. I have it in my program? The code above isn't a complete program,
I have to integrate it into your program. It still doesn't run
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just to be clear:
  • This line was on your original post #15
  • I forgot to add it in #16 because I corrected other things and edited the 'paste' after doing the 'copy'. So at the beginning it wasn't. Then I fixed it as I told you.

Now I don't know what is exactly what doesn't work for you.
If you need more help regarding something that does not work, please post a complete project (file -> export as zip) and the error that you are having. Otherwise, it will be difficult for me and other people in this forum to help you
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
I have sent a zip file of my whole program to this query. It is literally your Camera_NV21utils with the code above added, the button removed and
Dotty( PreviewPic ) inserted in Sub Camera1_Preview (PreviewPic() As Byte) at line 296. When I try to compile it, it says

Parsing code. 0.02
Compiling code. Error
Error compiling program.
Error description: Cannot cast type: {Type=Double,Rank=0, RemoteObject=False} to: {Type=Byte,Rank=1, RemoteObject=True}
Occurred on line: 296
Dotty( PreviewPic )
Word: previewpic

I am sorry if this troubles you. I still have a lot to learn.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Hi again,

In fact we are all here to learn and help. But it is not always easy for the others to follow. It is worth to try to understand some concepts even if it takes its time.

Back to the problem, I have taken the original Erel's example (from HERE ) again and added your code. As my NV21 lib is no longer needed, it is better to go back to the origin and clean not used things.

  • I have forced it to be landscape (so the byte order given by the PreviewPic() array will be the same as seen on the screen )
  • Also, in the example there is a class which is CameraEx. It is necessary to keep it, since all the called methods are from there. I would also recommend reading the post to understand a bit better how everything is made and also a bit of background in classes.
  • From the original example, now the buttons have been hidden in the designer properties. They are still there in case you will need to use some of them in the future.
  • There is a slider (which in the original example was used for the zoom), that now controls the threshold
  • Also some minor changes (colors,...) with no special reason.

Relevant code changes (respect to the original example) are at the end of Main module, and/or with comments "ADDED CODE"

I like the result
 

Attachments

  • CameraEx_modified.zip
    9 KB · Views: 308
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
Thank you for your understanding. I am unable to load this file, as I have B4A registered version 3.00 and I get messages

"This file is from a newer version" and "Error loading file" and "given key was not present in the dictionary"

I don't think I am entitled to any more free upgrades.

Enjoyed the spoiler video though. Seems just what I want. Except the dots seem a lot less dense.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Did the original example (the one with NV21) compile ok?

About the dots, now you are scanning one of each 10 rows and columns for speed. I changed the code so that it would draw a small circle instead of a dot, that's the reason.
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
Yes it did! That's whats confusing me and the cameraex library loaded too! but erels cameraex example routine wouldn't load either.
Maybe you have a solution?
See what you mean about the circles, but I need fine detail, as I am detecting two laser lines and nothing else (hopefully).
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
"This file is from a newer version" and "Error loading file" and "given key was not present in the dictionary"
It can be due to the layout file, which is not compatible backwards. If this is the case, it will be easy to remove layout dependencies since you don't need them.

Not sure about the "given key was not present in dictionary" (if it is also related to layout or not). Perhaps someone can help with this?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Can you test this one? I removed the designer layout and some other things that might cause the compatibility issue.

Now the dots are drawn again instead of circles. If you need more resolution you will need to lower the 'step' value used in 'Dotty' (currently 10, you could try with 5), but if you make it too small perhaps will experience performance issues.
 

Attachments

  • CameraEx_modified_noDesignerLayout.zip
    6.8 KB · Views: 291
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
I'm sorry no, it did exactly the same thing. I opened your NV21 program (which ran ok) and your cameraEx program (which didn't) in wordpad
as its the only way I could open it and took a screenshot of both side by side, which I hope you will be able to see. The preamble means nothing
to me except that the NV21 says version=3 and the cameraEx says version=6.5 . My B4A is version3. I wondered if you would be able to glean
anything from this screenshot.
 

Attachments

  • B4Acamera.jpg
    485.3 KB · Views: 299
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I could find my B4A v3.0 version and made a new project copying the project contents. I couldn't test it on the device, but at least it seems to compile ok
 

Attachments

  • cameraScan_compatible_V30.zip
    6.5 KB · Views: 297
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
You're a Genius JordiCP ! It works perfectly! I will go away and play with my new toy.
I'm sure I'll have more questions for you, but I will try to keep them to a minimum.
I am learning a lot here. Thanks a Million!
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
I have managed to get Dotty down to this

B4X:
Sub Dotty(PreviewPic() As Byte)
 cvsGraph.DrawColor(Colors.Black)
 
 For row = 0 To 480-1 Step 5
 For col = 0 To 640-1 Step 1
 luminance = PreviewPic( 640*row + col)
 If luminance>myThreshold Then
 cvsGraph.DrawPoint(col, row, Colors.White)
 End If
 Next
 Next
 DoEvents 'Sleep(0)
 pnlGraph.Invalidate
End Sub

Which speeds the process up a bit as I want to do Hi Res. I can get it to do every horizontal pixel but the vertical will
not do any less than every fifth row. Can you think why this is?
when only a low number of pixels are lit, the speed is quite good.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
not do any less than every fifth row. Can you think why this is?
Change the "step 5" in the first "For" loop to any other lower value

I think that it may be that the picture size is not 640x480. How can I force it to be that?
640x480 is the default for some android device cameras, but not for all, so this may be the case. You can modify the Camera1_Ready Sub to force it and show in the screen the real preview size.
B4X:
Sub Camera1_Ready (Success As Boolean)
   If Success Then
     camEx.SetJpegQuality(90)
     camEx.SetContinuousAutoFocus
     camEx.SetPreviewSize(640,480)    '<---- ADD THIS!!!!
     camEx.CommitParameters
     camEx.StartPreview
     Log(camEx.GetPreviewSize)
     ToastMessageShow(" Preview size is "& camEx.GetPreviewSize.Width & "," & camEx.GetPreviewSize.Height,True)     '<--- ALSO ADD THIS LINE 
   Else
     ToastMessageShow("Cannot open camera.", True)
   End If
End Sub

Tried camEx.SetPictureSize(640,480) but it brought up a java exception when run
This command is not for preview size but for the taken picture size. They are different things


About this line...
B4X:
luminance = PreviewPic( 640*row + col)
I don't think it is correct. It will give you problems. PreviewPic is a byte array. In B4A and Android, bytes are signed. It is no problem if you only use it as a container for data that you know that are unsigned, but if you compare them (in this case they are being compared to luminance), then the sign matters.

I mean, a normal point, when detected by the camera, can have a luminance of 100. It is a value 0x64 (in hexa) ..> as it is lower than 128, then it can be taken directly compared to luminance.
But if we have a bright point with a luminance of 192, that is 0xC0 in hexa.--> as it is stored in a byte, if you get its value it will be interpreted as negative. Then, when you compare it to the luminance threshold, the comparison will never be good.
.Even if it is slower, I would change that line and put instead again
B4X:
luminance = Bit.And( PreviewPic(640*row+col),0xFF)  '<-- It will give you an integer and always positive.
 
Upvote 0

ChocoScope

Member
Licensed User
Longtime User
Yes that fixed it. Thanks. I see what you mean about the luminance value. When I was working in a dark place with the lasers
everything was fine, but when I pointed the camera at my window I got a Halo effect. I have two choices. Either work in a dark
environment or put the luminance code back in. The speed is incredibly slow with lots of light (because of the resolution) but is
acceptable for seeing the laser beams (about 4 frames/sec). Thanks again
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…