![pexelsphoto139829v2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/pexels-photo-139829_v2.jpg?resize=867%2C694&ssl=1)
![pexelsphoto139829v2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/pexels-photo-139829_v2.jpg?resize=867%2C694&ssl=1)
C++ is a strong and faster programming language, this feature really important on dynamic operations running less than in milliseconds of intervals. C++ is one of the best to analyze video camera images, it is good to analyze video image operations by using very modern methods of programming.
In C++ Builder, it is easy to use camera of device and easy to to capture video camera images by using CameraComponent with it’s FireMonkey UI framework. There is a video camera sample in Samples folder., please check details here before you start coding.
This small example below show how to use CameraComponent. You just need to add an Image component to show camera buffer and drag CameraComponent on your C++ Builder Project Form and double click on CameraComponent to add OnSampleBufferReady event, add lines as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { CameraComponent1->Active=true; } //--------------------------------------------------------------------------- void __fastcall TForm1::CameraComponent1SampleBufferReady(TObject *Sender, const TMediaTime ATime) { CameraComponent1->SampleBufferToBitmap(Image1->Bitmap, True); // now we have shot from the camera, we can analyze bitmap here } |
After this step you can analyze Bitmap of Image1. You can analyze the image inside of this OnSampleBufferReady event. If your analyze takes much time, camera display may have delays while viewing actions, you can analyze this image in another Timer(), so your video frames will be more smoother while analyzing image takes much more time. That means you will pass some of frames to analyze, you will just show it.
In C++ Builder, Multi-Device FireMonkey projects are really good on Bitmap operations, we highly recommend FMX on Video Camera Operations, You can use same code on Windows, MacOS, iOS and Android. VCL Projects are also can be done in same way. FMX has locking Bitmap Data and reading mapped pixels are much more faster and it is possible to analyze or operate on Bitmaps.
In this example, this procedure can be used to find mean color of an image in a given rectangular area.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
TAlphaColorRec mean_color(TBitmapData *bmpData, TRect rect) { unsigned int x,y,n=0; unsigned long int R=0,G=0,B=0; TAlphaColorRec acr; for (y=rect.top; y<rect.bottom; y+=1) { for(x=rect.left; x<rect.right; x+=1) { acr.Color = bmpData->GetPixel (x, y); // get pixel color at x,y coords of given bitmap data R+=acr.R; G+=acr.G; B+=acr.B; n++; } } // lets find each mean ARGBs & return color value acr.R = R/n; acr.G = G/n; acr.B = B/n; acr.A = 255; // this is about mean transparency that we don't need return acr; } |
![camexample | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/cam_example.png?resize=675%2C475&ssl=1)
![camexample | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/cam_example.png?resize=675%2C475&ssl=1)
Now lets add a ColorBox on the form to show this calculated mean color. We can use this mean color procedure on the bitmap of image. Please update the first code to show mean color of camera image as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void __fastcall TForm1::CameraComponent1SampleBufferReady(TObject *Sender, const TMediaTime ATime) { TBitmapData bitmapData; TAlphaColorRec acolor; CameraComponent1->SampleBufferToBitmap(Image1->Bitmap, True); // now we have shot from the camera, we can analyze bitmap here, ... if( Image1->Bitmap->Map(TMapAccess::Read, bitmapData)) // Lock bitmap and retrive bitmap data { Image1->Bitmap->Unmap(bitmapData); acolor=mean_color(&bitmapData, TRect(0,0,Image1->Bitmap->Width-1,Image1->Bitmap->Height-1)); ColorBox1->Color=acolor.Color; } } |
![camexample2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/cam_example2.png?resize=602%2C410&ssl=1)
![camexample2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/12/cam_example2.png?resize=602%2C410&ssl=1)
This simple example can be applied to analyze images that is called image processing. Nowadays it is very important to analyze faces, people, motions etc. You can use this to analyze or to control things and to do effects for fun. l. You can also add more complex algorithms like Deep Learning methods or other Artificial Intelligence Technologies.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition