PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label image-segmentation. Show all posts
Showing posts with label image-segmentation. Show all posts

Wednesday, July 27, 2022

[FIXED] How to cheaply "undo" a mosaic (i.e. split an image into halves or quadrants) with ffmpeg?

 July 27, 2022     crop, ffmpeg, image-segmentation     No comments   

Issue

FFMPEG makes it easy to take multiple inputs and stack them into a mosaic video. I am looking for a way to do the opposite, specifically I would like take a video stream that is composed of four streams stacked into quadrants and split it into four separate videos consisting of the coordinates

video1 = [0, 0.5*w, 0, 0.5*h]
video2 = [0.5*w, w, 0, 0.5*h]
video3 = [0, 0.5*w, 0.5*h, h]
video4 = [0.5*w, w, 0.5*h, h]

I know that I can do this with four separate ffmpeg calls using the crop filter but this seems to be unnecessarily expensive. Is there a way to do this in a single call?


Solution

crop filter

You can use four crop filters in one command:

ffmpeg -i input -filter_complex "[0]crop=iw/2:ih/2:0:0[tl];[0]crop=iw/2:ih/2:ow:0[tr];[0]crop=iw/2:ih/2:0:oh[bl];[0]crop=iw/2:ih/2:ow:oh[br]" -map "[tl]" topleft.mp4 -map "[tr]" topright.mp4 -map "[bl]" bottomleft.mp4 -map "[br]" bottomright.mp4

bitstream filter

A bitstream filter is different than a normal filter. A normal filter requires decoding and encoding. A bitstream filter operates on the encoded stream data, and performs bitstream level modifications without performing decoding.

The h264_metadata and hevc_metadata bitstream filters can edit the window cropping offsets in the SPS for H.264 and H.265/HEVC. What this means is that it can change these values without needing to re-encode the video. The file size will remain the same but the player will crop the video according to the crop values you set.

Example for H.264 320x240 input:

ffmpeg -i input.mp4 -bsf:v h264_metadata=crop_right=160:crop_bottom=120 -c copy topleft.mp4 -bsf:v h264_metadata=crop_left=160:crop_bottom=120 -c copy topright.mp4  -bsf:v h264_metadata=crop_right=160:crop_top=120 -c copy bottomleft.mp4 -bsf:v h264_metadata=crop_left=160:crop_top=120 -c copy bottomright.mp4

These fields are set in pixels. Note that some sizes may not be representable if the chroma is subsampled (it basically means you should only use even values for your typical video).

To script this you can use ffprobe to get the width and height.



Answered By - llogan
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, May 6, 2022

[FIXED] How to judge the contour is line or curve by opencv?

 May 06, 2022     algorithm, image, image-processing, image-segmentation, opencv     No comments   

Issue

Test image

This image have two contours. I can find both with opencv findcontour function. What I want to know is how to judge which contour is line and which contour is curve ? Can anybody please tell how to do it?


Solution

Start by assuming you have a line, and apply some basic algebra.

First find the slope of the line and the y-intercept. The slope of a line is defined as the change in y divided by the change in x. So given two points (x0,y0), (x1,y1):

slope = (y0-y1) / (x0-x1)

The y-intercept is found using the slope-intercept equation (y=mx+b) and solving for b:

y = mx + b
b = y - mx

So

y_intercept = y0 - slope * x0

Once you have the slope and the y-intercept, you just need to loop through the points of your contour and see if all of the points fall on the same line. If they do, you have a line; if they don't, you have a curve.

Since I don't know what language you're working with, here is the whole process in pseudocode:

// First assume you have a line - find the slope and y-intercept
slope = (point[0].y - point[1].y) / (point[0].x - point[1].x);
y_intercept = point[0].y - (slope * point[0].x);

// Using slope-intercept (y = mx + b), see if the other points are on the same line
for (n = 0 to numPoints)
{
    if ((slope * point[n].x + y_intercept) != point[n].y)
    {
        // You've found a point that's not on the line - as soon as you
        // find a point that's not on the line, you know that the contour
        // is not a straight line
    }
}

Note that you'll be dealing with floating-point numbers here, so you'll have to take that into account in the if condition - you can't directly compare floating point numbers for equality, so you'll need to round them to some acceptable degree of accuracy. I left that out to keep the pseudocode simple.



Answered By - Jeff Loughlin
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing