is there a way to get information about the zoomed section in python cv2 ?
when using opencv in python (import cv2)
then using cv2.imshow('windowname', imgnparray)
then when i hover over the image and use the scroll wheel i can zoom into the image, in python can i somehow get information about the zoomed area?
in my example the zoom would be around 3 times zoomed in and 0.5 offset left and 0.25 offset top
That's currently not possible.
To get this information, you would need to modify OpenCV's source code. You could open an issue about this, requesting the feature.
The relevant values can be found in the variable positionCorners in the file modules/highgui/src/window_QT.cpp.
Related
I am developing an app for Android and i have this issue - after i have taken a picture with the camera, i need to crop it. But the coordinates for the rectangle i have dragged over the capturing area start from the screen's 0,0 coordinates aka touch coordinates do not match actual picture's - if i try to crop the image with these using PIL, i get a partial result.
One possible solution would be to take a partial screenshot with these coordinates and get the cropped picture that way.
I tried to use pyscreenshot, but then i found out that it does not work on Android.
Any ideas how to capture a partial screenshot on Kivy?
Thank you
For possible anyone with the same issue - i did not find any good way to grab a screenshot and crop selected portion out of it. I did, however, solve it this way - i move to a new screen, resize picture to device's screen resolution and from then i can crop it with touch without no issues - i did not have to find a clever way to map screen coordinates with image's. After cropping and saving i resize the image back to a fixed resolution. This is not a elegant solution, but still will serve my purpose.
Thank you anyone who thought along with me.
I have a Geotiff that I display on a tile map, but it's slightly off to the south. For example, on this screenshot the edge of the image should be where the country border is, but it's a bit to the south:
Here's the relevant part of the code:
tiff_rio_500 = rioxarray.open_rasterio('/content/mw/mw_dist_to_light_at_all_from_light_mask_mw_cut_s3_500.tif')
dataarray_500 = tiff_rio_500[0]
dataarray_500_meters = dataarray_500.copy()
dataarray_500_meters['x'], dataarray_500_meters['y'] = ds.utils.lnglat_to_meters(dataarray_500.x, dataarray_500.y)
hv_dataset_500_meters = hv.Dataset(dataarray_500_meters, name='nightlights', vdims='cumulative_cost')
hv_tiles_osm_bokeh = hv.element.tiles.OSM().opts(width=1000, height=800)
hv_image_500_meters_bokeh = hv.Image(hv_dataset_500_meters, kdims=['x', 'y'], vdims=['cumulative_cost'], rtol=1).opts(cmap='inferno_r')
hv_combined_osm_500_meters_bokeh = hv_tiles_osm_bokeh * hv_image_500_meters_bokeh
hv_combined_osm_500_meters_bokeh
You can see the live notebook on google colab.
Now this is not the usual "everything is way off" problem that occurs when one doesn't convert the map to Web Mercator. It is almost perfect, it just isn't.
The Geotiff is an Earth Engine export. This is how it looked originally in Earth Engine (see live code):
As you can see, the image follows the borders everywhere.
At first, I suspected that maybe the export went wrong, or the google map tileset is somewhat different, but no, if I open the same exported Tiff in the QGis application on my windows laptop and view it on the same OSM tilemap as I do in the colab notebook, it looks fine:
Okay, the image does not follow the borders perfectly, but I know why and that's unrelated (I oversimplified the country border geometry). The point is, that it is projected to the correct location. So based on that, the tiff contains the correct information, it can be displayed at the same location as the borders are in the OSM tilemap, but still in my Holoviews-Datashader-Bokeh project it is slightly off.
Any idea why this happens?
I've got the answer on the Holoviz Discourse from one of the developers. Seeing how the recommended function is practically undocumented, I copy it here in case somebody looks for an easy way to load a geotiff and add to a tilemap in Holoviews/Geoviews:
https://discourse.holoviz.org/t/geotiff-overlay-position-is-slightly-off-on-holoviews-bokeh-tilemap/2071
philippjfr
I wouldn’t expect manually transforming the coordinates to work
particularly well. While it’s a much heavier weight dependency for
accurate coordinate transforms I’d recommend using GeoViews.
img = gv.util.load_tiff( '/content/mw/mw_dist_to_light_at_all_from_light_mask_mw_cut_s3_500.tif' )
gv.tile_sources.OSM() * img.opts(cmap='inferno_r')
Edit: Now it is possible one doesn't want to use Geoviews as it has a pretty heavy dependency chain that requires a lot of patience and luck to set it up right. Fortunately rioxarray (through rasterio) has a tool to reproject, just append ".rio.reproject('EPSG:3857')" to the first line and then you don't have to use the lnglat_to_meters which is not intended for this purpose.
So the corrected code becomes:
tiff_rio_500 = rioxarray.open_rasterio('/content/mw/mw_dist_to_light_at_all_from_light_mask_mw_cut_s3_500.tif').rio.reproject('EPSG:3857')
hv_dataset_500_meters = hv.Dataset(tiff_rio_500[0], name='nightlights', vdims='cumulative_cost')
hv_tiles_osm_bokeh = hv.element.tiles.OSM().opts(width=1000, height=800)
hv_image_500_meters_bokeh = hv.Image(hv_dataset_500_meters, kdims=['x', 'y'], vdims=['cumulative_cost'], rtol=1).opts(cmap='inferno_r')
hv_combined_osm_500_meters_bokeh = hv_tiles_osm_bokeh * hv_image_500_meters_bokeh
hv_combined_osm_500_meters_bokeh
Now compared to the Geoviews solution (that supposedly handles everything automatically), this solution has a downside that if you use a Hover Tooltip to display the values and coordinates under the mouse cursor, the coordinates are showing up in the newly projected web mercator system in millions of meters instead of the expected degrees. The solution for that is outside the scope of this answer, but I'm just finishing a detailed step by step guide that contains a solution for that too, and I will link that here as soon as it is published. Of course if you don't use Hover Tooltip, the code above will be perfect for you without any more tinkering.
I would like to make a QT based GUI program which overlays a heatmap on an image from 20fps streaming FHD video.
The target image looks like this
(Additionally, a colorbar beside an overlayed image shall also be displayed.)
The size of heatmap source for each image is 100x40, and therefore interpolation for FHD(1920x1080) is needed per frame.
(FYI, The min and max values of heatmap source are around 10 and 100000, respectively.)
First of all, I used cv2.VideoCapture function in Opencv to get images from video. And then, I googled some examples to combine image and heatmap using Matplotlib such as:
https://github.com/durandtibo/heatmap
Heatmap on top of image
Overlay an image segmentation with numpy and matplotlib
The problem that I faced is processing speed to meet 20fps for FHD resolution.
It seemed that Opencv is more adequete rather than Matplotlib for real-time processing.
(I couldn't find the good way to show heatmap and colorbar using Pyqtgraph even though it provides high speed.)
So, I searched another way using cv2.applyColorMap and cv2.resize.
It looks like cv2.applyColorMap function doesn't automatically adjust the range of values unlike imshow function in Matplotlib, and therefore the color of result image is strange.
Moreover, Opencv image needs to be adopted to QtWidget using QtGui.QImage and QtGui.QPixmap which results additional delay.
Finally, the overall processing time of the method I searched can not meet the requirement.
Please show me the way for the solution.
Thanks in advance.
I want to take a screenshot of whole screen in python which is doable(feasible) to me. But if i want to take screenshot of a desired clipped area like the picture shown below means first of all, user should select(clip) a region using mouse pointer and then take screenshot of that area. So i want to say that how to clip a portion of an image in a clipboard?
NOTE: I have found some packages like "clipboard" and "pyperclip" but i couldn't understand how to clip an image? Help me
I got a code but it shows to clip only text. What modifications should be made in it to clip a desired area of an image.
import clipboard
clipboard.copy("abc")
text = clipboard.paste()
print(text)
I would find a python module that allowed you to edit image files, or use a binding to an external program. A tool like pyperclip will enable you to get at the image in python, you will then have to edit the image yourself using a module or something else.
Running Python, I have an image and some data calculated for different ROIs (regions of interest).
I would like to display that image, and have a tooltip pop up whenever I am over one of those regions of interest.
This is mainly for debugging purposes - so I don't care that things will be very pretty, or integrate into any other sort of GUI - just that I can easily understand what value I calculated for each part of the image.
Also - I don't mind which imaging/display library to use for that purpose. I normally work with PIL, or directly with numpy arrays - but other libraries are just as good for me.
Thanks!
If it's for debugging you can simply get the position of mouse clicks and print the value for the corresponding ROI. I would use OpenCV as it has SetMouseCallback() and you can define ROIs by polygons and then test what polygon gets the click, see this example. If you've never used OpenCV before then maybe this is not the best option.