Comments (14)
Hello, Thank you for your question.
-
As I described in comment in line 32, immediately after starting the USB camera,
GetBitmap() fails because image buffer is not prepared yet.
What you are saying is this situation? -
If not,
I don't know if it works, but could you try the following change around line 318?
And please let me know it work fine or not.
before change.
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}
lock (BufferLock)
{
Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
}
return 0;
}
after change.
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
lock (BufferLock)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}
Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
}
return 0;
}
from usbcamera.
Thanks for your suggestion, I modified the code as shown. The defect seems less frequent, moreover it does it especially when the camera is cold and it happens also with AMCAP. With their test program IC Capture (The Imagin Source) it never happens.
from usbcamera.
Hello.
- Could you try the following change too, from line 306.
before change:
GetBitmap(int width, int height, int stride)
{
if (Buffer == null) return EmptyBitmap(width, height);
lock (BufferLock)
{
return BufferToBitmap(Buffer, width, height, stride);
}
}
after change:
GetBitmap(int width, int height, int stride)
{
lock (BufferLock)
{
if (Buffer == null) return EmptyBitmap(width, height);
return BufferToBitmap(Buffer, width, height, stride);
}
}
- If the problem is not solved yot,
could you check VideoFormat your camera supports?
Execute below and let me know result.
// check format.
int cameraIndex = 0;
UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex);
for(int i=0; i<formats.Length; i++) Console.WriteLine("{0}:{1}", i, formats[i]);
from usbcamera.
Hello.
I found that it is necessary to return quickly in callback function.
if callback does not return quickly, it can interfere with playback.
https://docs.microsoft.com/en-us/windows/win32/directshow/isamplegrabber-setcallback
This is previous version document, it is prohibited to enter critical section.
https://docs.microsoft.com/en-us/previous-versions/ms786692(v=vs.85)
Could you try this version?
I changed do not enter critical section and do not wait another thread.
And please let me know your problem is solved.
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}
var locked = false;
try
{
System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
if (locked)
{
Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
}
}
finally
{
if (locked) System.Threading.Monitor.Exit(BufferLock);
}
return 0;
}
from usbcamera.
Hello,
I modified as follows:
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}
var locked = false;
try
{
System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
if (locked)
{
Marshal.Copy(pBuffer, Buffer, 0, BufferLen);
}
}
finally
{
if (locked) System.Threading.Monitor.Exit(BufferLock);
}
return 0;
}
But the image freeze with the cold board continues; then the board warms up and does not freeze anymore. I immediately thought of a hardware problem since it freezes with the old AMCAP; but as I told you this problem never happens with their (the Imaging Source) IC Capture 2.5 program. Anyway I bought a new camera-board that will arrive next month.For the moment I put a 200 ms timer that compares the previous frame with the next one (I reduce the frames to 1/16 to speed up), when they are identical (normally there is always some flickering) I give the commands Camera.Stop(), Camera.Start(). But it's not very nice.
from usbcamera.
AMCAP, as UsbCamera is based on DirectShow, while IC Capture is a software based on IC Imaging Control .NET Component for C# and VB.NET projects.
from usbcamera.
I also tried an old version of UsbCamera where there was no callback function but there was GetBitmapMain and GetBitmapMainMain, but even with that same problem. Waiting for the new camera-board to arrive to draw conclusions.
from usbcamera.
Thank you for your response.
One last thing I want to check.
Please execute following code and check output message.
// check format.
int cameraIndex = 0;
UsbCamera.VideoFormat[] formats = UsbCamera.GetVideoFormat(cameraIndex);
for(int i=0; i<formats.Length; i++) Console.WriteLine("{0}:{1}", i, formats[i]);
for example, this is my result.
0:[Video], [MJPG], {Width=1280, Height=720}, 333333, [VideoInfo], ...
1:[Video], [MJPG], {Width=320, Height=180}, 333333, [VideoInfo], ...
2:[Video], [MJPG], {Width=320, Height=240}, 333333, [VideoInfo], ...
...
9:[Video], [YUY2], {Width=1280, Height=720}, 1000000, [VideoInfo], ...
10:[Video], [YUY2], {Width=320, Height=180}, 333333, [VideoInfo], ...
11:[Video], [YUY2], {Width=320, Height=240}, 333333, [VideoInfo], ...
...
18:[Video], [RGB24], {Width=1280, Height=720}, 1000000, [VideoInfo], ...
19:[Video], [RGB24], {Width=320, Height=180}, 333333, [VideoInfo], ...
20:[Video], [RGB24], {Width=320, Height=240}, 333333, [VideoInfo], ...
Now, if you are using formats[0], (that is [MJPG]),
var camera = new UsbCamera(0, formats[0]);
please use same size but another format like formats[9] (that is [YUY2]) or formats[18] (that is [RGB24])?
from usbcamera.
I hope you can see
from usbcamera.
I use format #5
from usbcamera.
The camera is black and white, that's why I had chosen the Y800 1280x960 format. Now however, after setting the RGB24 1280x960 format it seems that the defect does not. I need to try longer, but it already seems significant.
from usbcamera.
I have one question. When you used format #5 (Y800), aside from occasional preview freeze, are you able to get correct image? (unbroken image?)
BufferToBitmap function works correctly if image memory layout is RGB24. But you are using Y800 format and it's memory layout is difference from RGB24.
from usbcamera.
Hi,
Yes with Y800 format #5 and also with Y16 format #1 I had a correct image. However, after the latest changes (format RGB24 format #9), on my development machine (CPU Intel Core i7 8 core 3.8 GHz) the problem did not occur anymore but, on a tablet with CPU Celeron Gemini Lake N4120 4 core 2.4 GHz instead it manifested itself. I'm waiting to receive the new camera to do more tests.
from usbcamera.
Thank you. I'm feeling little strangeness, but I accept the result.
I guess SampleGrabber (parts of DirectShow) converted Y16/Y800 to RGB24 appropriately.
I close this issue. If you have any question, ask me again, but I have not so much idea any more.
from usbcamera.
Related Issues (20)
- WPF 每次 BufferToBitmap 的时候都创建新的 WriteableBitmap 岂不是完全没发挥 WriteableBitmap 的性能 HOT 11
- Release taking long time sometimes HOT 1
- How can I get the datas of every frame? HOT 9
- missing events HOT 4
- How can I switch camera? HOT 2
- provide nuget package HOT 4
- Implement IDisposable / IObservable pattern HOT 1
- 需要增加对于部分非标的虚拟摄像头的过滤(It is necessary to increase the filtering of some non-standard virtual cameras) HOT 7
- Problem with showing preview HOT 4
- Memory issues HOT 13
- How to distinguish the camera with the same name? HOT 4
- Add a feature to get plain image data as byte array to not call lock bits over and over again HOT 15
- threads in thread pool may be blocked by this library HOT 4
- Inconsistent Image Quality When Saving Still Capture Monochrome Sensor Data as BMP HOT 8
- Can't able to set brightness and exposure at run time when camera running on different thread HOT 2
- Capture Pin not working on all cameras HOT 3
- happens ArgumentException when I dispose PictureBox's old image. HOT 1
- How can I turn off the usbcamera lights? HOT 9
- why not working in 4K? HOT 6
- Add an IsReady method to see if we can get the image? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from usbcamera.