Python, plotting curve into image from pixels - image

I need help, I have a matrix of pixels of values 0 to 255 and I need to plot them into image with plane curve.
Into this image I need to put a specific curve.
For example, I have a curve x=10+cos(t), y=10+sin(t) and a matrix of pixels m[i][j]. I need to show both in one image, looking like that:
I know I might need opencv, it's not problem, but I can't find myself writing the code.

You are showing a circle.
If you need to draw a circle, use the cv::circle drawing call.
API docs: https://docs.opencv.org/4.x/d6/d6e/group__imgproc__draw.html
If you need an ellipse, use the ellipse drawing call.
If you need to draw arbitrary polylines/drawContours, calculate the list of points and then use either of these calls.
OpenCV has a Plot2D class in the opencv_contrib repository/part of the modules. That's not parametric plot though.

Related

Using regionprops in MATLAB to detect shapes only in part of the picture

I have video frames with elliptical objects in them. I'm trying to detect the main ellipse using regionprops and it works just fine.
However, since I want to speed up the process, I want regionprops to only look for those ellipses in a certain area of the image. I can crop the images each frame, to only have the relevant area left, but I would rather have regionprops look only in specified areas.
Is such an option possible?
Regioprops uses label matrix provided by bwlabel(bw_image) or just logical(grayscale_image) if you suppose that there is only object in the image. To make regionprops process only a part of image you should set irrelevant part of the label matrix to zero.

Image pixelation library, non-square "pixel" shape

I've seen a few libraries that pixelate images, some of them even feature non-square shapes such as The Pixelator's circle and diamond shapes.
I'm looking however to make a particular shape, I want a "pixel" that is 19x27 px. Essentially, the image would still look pixelated but it would use tallish rectangle shapes as the pixel base.
Are there any libraries out there that do this, if not, what alterations to existing algorithms/functions would I need to make to accomplish this?
Unless I am not understanding your question, the algorithm you need is quite simple!
Just break your image up into a grid of rectangles the size you want (in this case 19x27). Loop over each section of the grid and take the average color of the pixels inside (you can simply take the average of each channel in RGB independently). Then set all of the pixels contained inside to the average color.
This would give you an image that is the same size as your input. You could of course resize your image first to a more appropriate output size.
You might want to look up convolution matrices.
In a shader, you would use your current pixel location to grab a set of nearby pixels from the original image to render to a pixel in a new buffer image.
It is actually just a slight variation of the Box Blur image processing algorithm except that instead of grabbing from the nearby pixels you would grab by the divisions of the original image relative to the 19x27 divisions of the resulting image.

Best Elliptical Fit for irregular shapes in an image

I have an image with arbitrary regions shape (say objects), let's assume the background pixels are labeled as zeros whereas any object has a unique label (pixels of object 1 are labeled as 1, object 2 pixels are labeled as 2,...). Now for every object, I need to find the best elliptical fit of its pixels. This requires finding the center of the object, the major and minor axis, and the rotation angle. How can I find these?
Thank you;
Principal Component Analysis (PCA) is one way to go. See Wikipedia here.
The centroid is easy enough to find if your shapes are convex - just a weighted average of intensities over the xy positions - and PCA will give you the major and minor axes, hence the orientation.
Once you have the centre and axes, you have the basis for a set of ellipses that cover your shape. Extending the axes - in proportion - and testing each pixel for in/out, you can find the ellipse that just covers your shape. Or if you prefer, you can project each pixel position onto the major and minor axes and find the rough limits in one pass and then test in/out on "corner" cases.
It may help if you post an example image.
As you seem to be using Matlab, you can simply use the regionprops command, given that you have the Image Processing Toolbox.
It can extract all the information you need (and many more properties of image regions) and it will do the PCA for you, if the PCA-based based approach suits your needs.
Doc is here, look for the 'Centroid', 'Orientation', 'MajorAxisLength' and 'MinorAxisLength' parameters specifically.

Map image/texture to a predefined uneven surface (t-shirt with folds, mug, etc.)

Basically I was trying to achieve this: impose an arbitrary image to a pre-defined uneven surface. (See examples below).
-->
I do not have a lot of experience with image processing or 3D algorithms, so here is the best method I can think of so far:
Predefine a set of coordinates (say if we have a 10x10 grid, we have 100 coordinates that starts with (0,0), (0,10), (0,20), ... etc. There will be 9x9 = 81 grids.
Record the transformations for each individual coordinate on the t-shirt image e.g. (0,0) becomes (51,31), (0, 10) becomes (51, 35), etc.
Triangulate the original image into 81x2=162 triangles (with 2 triangles for each grid). Transform each triangle of the image based on the coordinate transformations obtained in Step 2 and draw it on the t-shirt image.
Problems/questions I have:
I don't know how to smooth out each triangle so that the image on t-shirt does not look ragged.
Is there a better way to do it? I want to make sure I'm not reinventing the wheels here before I proceed with an implementation.
Thanks!
This is called digital image warping. There was a popular graphics text on it in the 1990s (probably from somebody's thesis). You can also find an article on it from Dr. Dobb's Journal.
Your process is essentially correct. If you work pixel by pixel, rather than trying to use triangles, you'll avoid some of the problems you're facing. Scan across the pixels in target bitmap, and apply the local transformation based on the cell you're in to determine the coordinate of the corresponding pixel in the source bitmap. Copy that pixel over.
For a smoother result, you do your coordinate transformations in floating point and interpolate the pixel values from the source image using something like bilinear interpolation.
It's not really a solution for the problem, it's just a workaround :
If you have the 3D model that represents the T-Shirt.
you can use directX\OpenGL and put your image as a texture of the t-shirt.
Then you can ask it to render the picture you want from any point of view.

CvBOX2D Processing

I've already got my ROI(CvBOX2D type) by series of contour processing, now I just want to focus on the image part within the ROI, e.g.: feed this part into another processing function, how can I do that? I know there is CvSetImageROI, but the type is CvRect, so I should convert CvBox2D to CvRect first? Or some way to apply a mask on it with the area outside the box set to 0?
Thanks in advance!
Only axis aligned ROIs are directly supported in OpenCV (CvRect or IplROI). This is because they allow direct access to the image memory buffer.
There are 2 ways to go about working on a non-axis aligned ROI in OpenCV. Neither of them is as efficient as using axis-aligned ROIs.
Rotate your image, or bounding box, so that your ROI is now axis aligned in the resulting rotated image.
Note: the rotation will slightly blur your image.
Use a mask: Draw your ROI as a white rectangle on a black BG the same size as the image, and give your processing functions this mask as the additional parameter.
Note: not all functions support masks.
I would recommend option 1 if you really must stay within the exact bounds of your ROI. Otherwise, just use the bounding rect.
Use c++ api of opencv. seriously. do it.
cv::Rect roi = cv::RotatedRect(box).boundingRect();
Mat_<type> working_area(original_mat, roi);
// now operate on working_area
Note: this will operate on the bounding rect. I didn't find information on how to create a mask out of rotatedrect. Probably you have to do it by hand in a scanline fashion.

Resources