Helpful IronPython libraries

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Helpful IronPython libraries

Post by oopfan » Tue Jul 11, 2017 3:53 pm

Hi Robin,

I am in the process of upgrading a 50-year old GEM with a stepper motor on the RA axis. Previously it used a synchronous motor at a fixed rate determined by the power line frequency. Even with precise polar alignment the tracking suffers from periodic error in the worm gear. In my stepper motor solution I've written Python code to correct for periodic error but it requires me to enter "learning mode". For eight minutes my life is hell as I tell the drive to speed-up and slow-down as I endeavor to keep a star centered in crosshairs. It is exhausting work. It sure would be nice if I could automate this.

I've browsed the docs and see that I can command SC to capture a frame. That is great. I also see that I can open that file and write annotations. Super! So what I would like to do through script is to open the file and read a 50x50 pixel square at the center of the image, and then find a disc that qualifies as a star. I want to determine the (x,y) coordinates of the center of the disc, and record it to a file. Then, one second later I want to capture a new frame, and use the coordinates of the disc's center from the previous frame to look for a disc with a 50x50 pixel square around that center point. (I hope I am making sense.) Clearly, I could do this entirely offline using most any programming language. Since SC has a Python automation interface, it seems like something I might want to try first.

So my question is: Do you know of any third party Python libraries that can do a lot of the work of finding the star's disc? I could write it myself but if there is already something out there then I would like to use it.

Thank you!

User avatar
admin
Site Admin
Posts: 3171
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Helpful IronPython libraries

Post by admin » Tue Jul 11, 2017 6:13 pm

Hmm, there are useful python tools for this (like AstroPy and OpenCV), but neither will work in IronPython :(

Fortunately though it's all built in to SharpCap and with a little (undocumented until now) scripting you can get access to it

Code: Select all

import clr
clr.AddReference("SharpCap.ImageProcessing")
from SharpCap.ImageProcessing import OpenCVStarDetector
ocvsd = OpenCVStarDetector(1,16,100,0.0) 
clr.AddReference("System.Drawing")
from System.Drawing import Bitmap
bm = Bitmap(640,480)
ocvsd.Detect(bm)
The parameters for creating the OpenCVStarDetector are (minSize, maxSize, maxStarCount, gaussianBlurSigma). The .Detect() method takes a bitmap you want to detect stars in and returns an array of StarInfo (which have Position and Brightness among other useful properties).

cheers,

Robin

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Helpful IronPython libraries

Post by oopfan » Tue Jul 11, 2017 6:49 pm

Wow that's awesome, Robin! (That's what I was hoping you were going to say but I did not want to be too forward by asking.)

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Helpful IronPython libraries

Post by oopfan » Wed Jul 12, 2017 10:22 pm

Hi Robin,

Should I be concerned with what image file format I store the images in? I've got an ASI120MC. Will OpenCVStarDetector work equally well when processing RGB24, RAW8, and MONO8? Although color is unimportant to me for what I aim to do, it would be nice to capture in color for some unforeseen reason down the road.

Thanks,
Brian

User avatar
admin
Site Admin
Posts: 3171
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Helpful IronPython libraries

Post by admin » Thu Jul 13, 2017 6:05 pm

If you save in RAW formats then the star detection probably won't work as well as it should because of the grid pattern superimposed on the image by the Bayer matrix. One possible workaround for this would be to reduce the image size by a factor of 2 in each direction after loading and before star detecting (average out the grid pattern).

Colour or mono doesn't matter - the image is turned to mono anyway before the star detection algorithm is used.

cheers,

Robin

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Helpful IronPython libraries

Post by oopfan » Mon Aug 14, 2017 4:53 am

Hi Robin,

I've developed a number of useful scripts based on calls to OpenCVStarDetector. The problem I am having is that it is not very sensitive at all. I can send you snapshots but I can summarize by saying that I clearly see 7 stars but it struggles to find 3 maybe 4 if it is lucky. Here is how I've been calling it:

ocvsd = OpenCVStarDetector(1,20,1000,0.0)
SharpCap.SelectedCamera.CaptureSingleFrameTo(path)
bm = Bitmap(path)
stars = ocvsd.Detect(bm)

Tonight, I played around with the last argument: gaussianBlurSigma, using numbers between 0.0 and 0.9 but it only seemed to make things marginally better.

Is there a way to make the detector more sensitive? I don't mind getting false positives -- my algorithm is fairly robust.

Perhaps there are functions that I can call on the bitmap to digitally boost the gain? I am already using gain 100 on my camera with a 2 second exposure. These are exactly the settings I use for polar alignment and it finds many more stars. For my application I was hoping to operate at 0.5 second exposure, so I am a long way away from that goal.

Thank you,
Brian

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Helpful IronPython libraries

Post by oopfan » Mon Aug 14, 2017 12:43 pm

Hi Robin,

I think that you mentioned that the documentation of the "SharpCap.ImageProcessing" assembly is a work-in-progress. Do you know if there is anything in there that would allow me to stretch an image? Hopefully that will give OpenCVStarDetector more meat to sink its teeth into.

Thanks

User avatar
admin
Site Admin
Posts: 3171
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Helpful IronPython libraries

Post by admin » Tue Aug 15, 2017 7:10 am

Hi,

I'm afraid I'm away at the moment and don't have access to the source code to check on this for you. Will have a look when I get back at the end of the week.

Cheers.
Robin

User avatar
oopfan
Posts: 456
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Helpful IronPython libraries

Post by oopfan » Tue Aug 15, 2017 1:16 pm

No worries, Robin. I got some very helpful advice from Dave. With some luck the skies will be clear tonight. I am going to play around with the camera's brightness and gamma settings to try to get OpenCVStarDetector to see more stars.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest