Related
I'm using the H.264 library to compress a video frame by frame. It works, I can replay it back locally without any issue.
However, I need to send that video over the LAN and that LAN is rather busy already so I need to limit the size of each frame to a maximum of about 250Kb.
I use the following code to setup the parameters, but changing the bit rate values does not seem to have any effect on what the library does with the input frames:
x264_param_t param = {};
if(x264_param_default_preset(¶m, "faster", nullptr) < 0)
{
return -1;
}
param.i_csp = X264_CSP_I420;
param.i_width = 3840;
param.i_height = 2160;
param.i_keyint_max = static_cast<int>(f_frame_header.f_fps);
param.i_threads = X264_THREADS_AUTO;
param.b_vfr_input = 0;
param.b_repeat_headers = 1;
param.b_annexb = 1;
// the following three parameters are the ones I tried to change with no results
param.rc.i_bitrate = 100000;
param.rc.i_vbv_max_bitrate = 100000;
param.rc.i_vbv_buffer_size = 125000;
if(x264_param_apply_profile(¶m, "high") < 0)
{
return -1;
}
...enter loop reading frames and compressing them...
Changing the i_bitrate, i_vbv_max_bitrate and i_vbv_buffer_size parameters seems to have absolutely no effect on the size of the resulting frames. I still get some frames over 500Kb and in many even, rather large frames one after the other as the following sizes show:
20264
358875
218429
20728
25215
310230
36127
9077
29785
341541
222778
23542
21356
276772
25339
32459
421036
11179
6172
286070
193849
What I would need is the largest frame to be around 250,000 at its maximum. Now I understand that once in a while it go over a bit, but not 2×. That's just too much for my current available bandwidth.
What am I doing wrong in the parameters setup above?
I've seen this command line:
ffmpeg -i input -c:v libx264 -b:v 2M -maxrate 2M -bufsize 1M output.mp4
which would suggest that what I'm doing above should work (I tried all sorts of values including the ones one that command line). Yet the frame size does not really change between my runs.
I tried with a blur applied to each frame to see whether it work help. Yes! It did. The result is a movie which is 2.44 times smaller than the original.
To load each JPEG image from the original, I use ImageMagick++ (in C++), so I just do the following blur on each image:
image.blur(0.0, 5.0);
and that took about 10 hours total (without the blur the same processing took about 40 minutes) but it was worth it since in the end the compressed movie went from 1,293,272,023 bytes to only 529,556,265 bytes (2.44218 times smaller). The blur added about 3.3 seconds of processing per frame and there are a little over 11,000 frames in the original.
Note: I used 5.0 for the blur because I have 4K images and although I can see a sharp difference when I look at one frame, when playing back the resulting movie, I don't notice the final blur. If you have smaller images, you probably want to use a smaller number. It looks like many people use a blur of just 0.05 and already have good results in compression ratios.
In C, use the BlurImage() function:
Image *BlurImage(const Image *image,const double radius,
const double sigma,ExceptionInfo *exception)
Here are some references about using a blur to further compress JPEG images as it helps eliminates sharp edges which do not compress well in the JPEG format (as sharp edge are not as natural):
Recommendation for compressing JPG files with ImageMagick
How do I reduce the file size of an image? (search on "blur" to find the section)
Could I blur an image to dramatically reduce the file size?
I'm trying to resize a very small image using Octave 5.2.0 but I'm not sure why it's giving an error when trying to resize it
The error happens with the line below:
img_unique_only = imresize(f2, [640, 480]); %reshape output of unique colors
error: interp2: cubic requires at least 2 points in each dimension
error: called from
interp2 at line 244 column 9
imremap at line 65 column 19
imresize at line 135 column 8
test_small_resize_question at line 30 column 17 (the last line which is the imresize line)
Code Below:
pkg load image
f(:,:,1)=[0;0;0;0;127;128;128;128];
f(:,:,2)=[0;0;127;128;0;0;0;0];
f(:,:,3)=[127;128;0;0;0;0;127;128];
%%%%%%%%%%%%%%%%-------------------------------------------------------------------------
[im_r im_c]=size(f);
size_min=min(im_r,im_c); %get minum size from row and col
f2=uint8(f)
imshow(f2)
img_unique_only = imresize(f2, [640, 480]); %reshape output of unique colors
Using imshow(f2) creates the image below
The Line img_unique_only = imresize(f2, [640, 480]); %reshape output of unique colors won't resize it
Variables created:
When using the line img_unique_only = imresize(f2, [640, 480], "nearest"); %reshape output of unique colors
Variables created:
I get a gray scale image instead of a 640x480 color image
Note: I'm also willing to try another way if better
A work around is to use cat Note: it creates a gradient of colors that aren't correct.
f(:,:,1)=[0;0;0;0;127;128;128;128];
f(:,:,2)=[0;0;127;128;0;0;0;0];
f(:,:,3)=[127;128;0;0;0;0;127;128];
height_wanted=640;
width_wanted=480;
repmat_rgb=cat(2,f,f); %add another column to array to get imresize to work
reshaped_output = imresize(repmat_rgb, [height_wanted, width_wanted],'cubic'); %reshape swatch to large output
imshow(reshaped_output);
Update: July 19 2021
It's a bug in imresize for more info and workaround
Matrix array to multidimensional RGB image array and using imresize to reshape image
I'm trying to create a DPI aware app which responds to user requested DPI change events by resizing the window.
The program in question is created in C and uses SDL2, however to retrieve system DPI information I use xlib directly, as the SDL DPI support in X11 is lacking.
I found two ways to get the correct DPI information on program startup, both involving getting Xft.dpi information from Xresource: one is to use XGetDefault(display, "Xft", "dpi"), while the other is to use XResourceManagerString, XrmGetStringDatabase and XrmGetResource. Both of them return the correct DPI value when the program is created.
The problem is, if the user changes the system scale while the program is running, both XGetDefault abd XrmGetResource still return the old DPI value even though when I run "xrdb -query | grep Xft.dpi" the value has indeed changed.
Does anyone know a way to get the updated Xft.dpi value?
I found out a way to do exactly what I wanted, even though it's rather hackish.
The solution (using XLib) is to create a new, temporary connection to the X server using XOpenDisplay and XCloseDisplay, and poll the resource information from that new connection.
The reason this is needed is because X fetches the resource information only once per new connection, and never updates it. Therefore, by opening a new connection, X will get the updated xresource data, which can then be used for the old main connection.
Be mindful that constantly opening and closing new X connections may not be great for performance, so only do it when you absolutely need to. In my case, since the window has borders, I only check for DPI changes when the title height has changed, as a DPI change will change the size of your title border due to font size differences.
First off it must be noted that the value of the Xft.dpi resource isn't necessarily accurate -- it depends on whether the system and or user login scripts have correctly set it or not.
Also it is important to remember that the Xft.dpi resource is intended to be used by the Xft library, not by arbitrary programs looking for the screen resolution.
The Xft.dpi resource can be set as follows. This example effectively only deals with a display with a single screen, and note that it uses xdpyinfo. This also shows how it might not be exact, but could be rounded. Finally this example shows calculation of both the horizontal and vertical resolution, but Xft really only wants the horizontal resolution:
SCREENDPI=$(xdpyinfo | sed -n 's/^[ ]*resolution:[ ]*\([^ ][^ ]*\) .*$/\1/p;//q')
SCREENDPI_X=$(expr "$SCREENDPI" : '\([0-9]*\)x')
SCREENDPI_Y=$(expr "$SCREENDPI" : '[0-9]*x\([0-9]*\)')
# N.B.: If true screen resolution is within 10% of 100DPI it makes the most
# sense to claim 100DPI to avoid font-scaling artifacts for bitmap fonts.
if expr \( $SCREENDPI_X / 100 = 1 \) \& \( $SCREENDPI_X % 100 \<= 10 \) >/dev/null; then
FontXDPI=100
fi
if expr \( $SCREENDPI_Y / 100 = 1 \) \& \( $SCREENDPI_Y % 100 \<= 10 \) >/dev/null; then
FontYDPI=100
fi
echo "Xft.dpi: ${FontYDPI}" | xrdb -merge
I really wish I knew why Xft didn't at least try to find out the screen's resolution itself instead of relying all of the time on its "dpi" resource being set, but I've found that the current implementation only uses the resource setting, so something like the above is actually always necessary to set the resource properly (and further one must also make sure the X Server itself has been properly configured with the correct physical screen dimensions).
From a C program you want to do just what xdpyinfo itself does and skip all the nonsense about Xft's resources. Here's the xdpyinfo code paraphrased:
Display *dpy;
dpy = XOpenDisplay(displayname);
for (scr = 0; scr < ScreenCount(dpy); scr++) {
int xres, yres;
/*
* there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
*
* dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
* = N pixels / (M inch / 25.4)
* = N * 25.4 pixels / M inch
*/
xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) /
((double) DisplayWidthMM(dpy, scr))) + 0.5;
yres = ((((double) DisplayHeight(dpy, scr)) * 25.4) /
((double) DisplayHeightMM(dpy, scr))) + 0.5;
}
XCloseDisplay(dpy);
Note also that if you are for some odd reason scaling your whole display (e.g. with xrandr), then you should want the fonts to scale equally with everything else. It's just a horrible bad hack to use whole-screen scaling to scale just the fonts, especially when for most things it's simpler to just tell the application to use properly scaled fonts that will display at a constant on-screen point size (which is exactly what Xft uses the "dpi" resource to do). I'm guessing Ubuntu does something stupid to change the screen resolution, e.g. using xrandr to scale up the apparent size of icons and other on-screen widgets without applications having to know about screen size and resolution, then it has to lie to Xft by rewriting the Xft.dpi resource.
Note that if you avoid whole-screen scaling then applications that don't use Xft can still get proper font scaling by correctly requesting a properly scaled font, i.e. even with bitmap fonts you can get them scaled to the proper physical on-screen size by using the screen's actual resolution in the font-spec. E.g. continuing from the above shell fragment:
# For pre-Xft applications we can specify physical font text sizes IFF we also tell
# it the screen's actual resolution when requesting a font. Note the use of the
# rounded values here.
#
DecentDeciPt="80"
DecentPt="8"
export DecentDeciPt DecentPt
#
# Best is to arrange one's font-path to get the desired one first, but....
# If you know the name of a font family that you like and you can be sure
# it is installed and in the font-path somewhere....
#
DefaultFontSpec='-*-liberation mono-medium-r-*-*-*-${DecentDeciPt}-${FontXDPI}-${FontYDPI}-m-*-iso10646-1'
export DefaultFontSpec
#
# For Xft we have set the Xft.dpi resource so this allows the physical font size to
# be specified (e.g. with Xterm's "-fs" option) and for a decent scalable font
# to be chosen:
#
DefaultFTFontSpec="-*-*-medium-r-*-*-*-*-0-0-m-*-iso10646-1"
DefaultFTFontSpecL1="-*-*-medium-r-*-*-*-*-0-0-m-*-iso8859-1"
export DefaultFTFontSpec DefaultFTFontSpecL1
# Set a default font that should work for everything
#
eval echo "*font: ${DefaultFontSpec}" | xrdb -merge
Finally here's an example of starting an xterm (that's been compiled to use Xft) with the above settings (i.e. the Xft.dpi resource and the shell variables above) to show text at physical size of 10.0 Points on the screen:
xterm -fs 10 -fa $DefaultFTFontSpec
You could try to use xdpyinfo(1); on my system it outputs, among a lot of other things:
dimensions: 1280x1024 pixels (332x250 millimeters)
resolution: 98x104 dots per inch
depths (7): 24, 1, 4, 8, 15, 16, 32
I don't know whether it can help you because I don't know how do you change the DPI of your screen, but chances are it works. Good luck!
--- UPDATE after comment ---
In a comment below from the OP, it is said that "there is a setting to change the DPI"... still I don't know which. Anyway, I tried Ctrl+Alt+Plus and Ctrl+Alt+Minus to change the resolution of the X server on the fly. After having changed the resolution, and seeing everything bigger than before, I ran xdpyinfo again. IT DIDN'T WORK: still the same output. But may be the method you use (which?) instead works...
I'm working on a "GS Wrapper" (using the 9.20 SDK) for use by an external application. There i scale down for example a A0 Sheet to A1, A2 and A3 and it works fine. (PDF to PS, then Print)
Problem: When i scale down any input format to A4, the printer cut off the borders of the content (these are technical drawings with a black border each 5mm from the sheet edge).
Is there an opportunity to scale down the A4 (to A4) again about 95% and center the image? (This should be result in a smaller base image, say the black borders are about ~10mm away from the sheet border afterwards)
I use the following parameter for scaling:
GhostArg[0] = "-dNOPAUSE";
GhostArg[1] = "-dBATCH";
GhostArg[2] = "-dSAFER";
GhostArg[3] = "-dNOPAUSE";
GhostArg[4] = "-g2480x3508";
GhostArg[5] = "-dPDFFitPage";
GhostArg[6] = "-r300x300";
GhostArg[7] = "-sDEVICE=ps2write";
GhostArg[8] = Output;
GhostArg[9] = Input;
Solution Update:
I managed to fix this problem by insert this three lines between Arg[8] and Arg[9]:
GhostArg[9] = "-c";
GhostArg[10] = "<< /BeginPage { 0.99 0.99 scale 10 10 translate } >> setpagedevice";
GhostArg[11] = "-f";
Thanks to KenS for the /BeginPage hint.
It sounds like your printer has a non-printable area. This is not uncommon, the paper handling needs to hold the paper while its being printed, and this can lead to some areas of the media not being printable.
If your content reaches to the edge of the media, its possible that the printer simple cannot print there, resulting in the content being cropped.
It is entirely possible to have ps2write drop the media content to a smaller size, but you can't have it (automatically) scale down and also shift the content location, because the content is fitted to the media size.
However, the FitPage mechanism doesn't look at the content, just the media size requests. So if the input requests A3 and the selected media is A4 (and fixed) then a scale factor is applied to scale the content to the required media size (and the media request for A3 is ignored).
So what you could do is leave the code you have as it is as present, but add a BeginPage or Install procedure which uses the scale operator to further reduce the size of marks on the page, and the translate operator to move the origin slightly so that the final content is centered.
Something like (example only, untested):
<<
/BeginPage {
0.95 0.95 scale
16 20 translate
}
>> setpagedevice
By the way, you do realise Ghostscript is licenced under the AGPL ?
Also, I'd very strongly recommend that you do not use the -g and -r switches, but instead simply use -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS to alter the media size.
The -g switch works in pixels, but high level output devices (eg pdfwrite and ps2write) don't emit pixels, they write high level vector objects. However, due to differences in the PostScript and PDF graphics models, some elements do need to be rendered to images and enclosed in that fashion in the PostScript output. By setting the resolution to 300 you are fixing the resolution at which those elements (eg pages containing transparency) are rendered. I'd suggest that you don't do so, unless you are working in a very tightly controlled workflow and know the resolution of the final output.
By using the DEVICEHEIGHTPOINTS and DEVICEWIDTHPOINTS switches you can control the media size without reference to the resolution. Note that in PostScript (and PDF) 1 point = 1/72 inch.
Hi iI have a small GUI that contains 1 'Push Button' and 3 'Edit Texts' and a few static text labels to display the results.
What I want to do is to be able to calculate from a series of numbers their: sum, average, min, max, Standard Deviation and Skewness
The user will enter the following data [using Edit Text boxes]:
Start Number of the sequence
End Number of the sequence
Increment step
And by using a Pushbutton all the above results will be returned in separate static texts.
I am very new to MATLAB can anyone push me into the direction i need to go inorder to achieve this.
My user interface if any help:
A simple solution should be :
function pushbutton1_Callback(hObject, eventdata, handles)
%[
startValue = str2num(get(handles.edit1,'string')) ;
stopValue = str2num(get(handles.edit2,'string')) ;
step = str2num(get(handles.edit3,'string')) ;
series = startValue:step:stopValue ;
average = mean(series) ;
minValue = min(series) ;
...
...
set(handles.text1,'string',average);
set(handles.text2,'string',minValue);
...
%]
Hope it will be helpful !
You might find these 41 complete GUI examples useful...
It'll answer you these questions:
1.How do I manipulate the strings in a uicontrol? GUI_1, 2, 4, 5, 13, 14, 15, 20, 21, 22, 37
2.How do make a uicontrol invisible/visible? GUI_3, 35 (See also GUI_10 for images)
3.How do I make a multi-line edit box? GUI_4
4.How can I initialize an editbox so that the cursor is blinking at startup? GUI_4, 24, 37
5.How can I let the user of my GUI know his actions are futile (or producing no results)? GUI_5
6.How can I tell which uicontrol is selected e.g., radiobuttons? GUI_6, 8
7.How do I tell how many times a uicontrol has been activated? GUI_7, 19, 28, 32, 33
8.How do I tell which button in a buttongroup is selected? GUI_8
9.How do I let the user know a process is running in the background? GUI_9
10.How can I set an image visible/invisible? GUI_10
11.How can I use a GUI to exit a FOR loop? GUI_11
12.How can I control the mouse pointer with a GUI? GUI_12
13.How do I access the value (current position) of a slider? GUI_13, 16
14.How do I use different colored strings in a listbox? GUI_14
15.What is the difference between 'listboxtop' and 'value' in a listbox? GUI_14
16.How do I make text that can be copied but not changed? GUI_15
17.How can I allow the user of my GUI to set the range of a slider? GUI_16
18.How can I display a digital clock in my GUI? GUI_17
19.How can I use a timer in a GUI? GUI_17
20.How do I use the buttondownfcn on an axes object? GUI_18, 28
21.How do I make a callback talk to another callback? GUI_19
22.How can I get the string from a popup or listbox? GUI_14, 20, 21, 22, 31, 32, 33
23.How can I set the string in a popup or listbox? GUI_21, 22
24.How can I add to the string in a popup or listbox? GUI_22
25.How do I tell which figure/axes was current before my callback executed? GUI_23
26.How do I get data from another GUI? GUI_24
27.How do I make a GUI to open image files only? GUI_25
28.How can I make popup choices mutually exclusive? GUI_26
29.How can I show the current pointer location in axes coordinates? GUI_27
30.How can I use uicontextmenus? GUI_28, 33, 39
31.How do I make my GUI control an axes in another figure? GUI_29, 30
32.What are callback strings? GUI_30
33.How can I make it so that when one of the figures closes, they all close? GUI_24, 29,
30, 41
34.How do I make several uicontrols interact in a more complicated GUI? GUI_31, 32, 33, 41
35.How do I get data from a GUI to the base workspace? GUI_25, 32, 33, 36
36.How can I use a GUI to take a screenshot of my desktop? GUI_34
37.How do I make toggle buttons act like tabbed-panels? GUI_35
38.How do I make a custom dialog box which returns a string to the base workspace? GUI_36
39.How can I make a password editbox that has the * symbols? GUI_37
40.How can I use nested function as callbacks? GUI_11, 17, 34, 36, 37, 39, 40, 41.
41.How can I use uiwait in a GUI? GUI_11, 34, 36, 37
42.How do I use JAVA in my GUI? GUI_38
43.How do I force the figure to maintain focus between uicontrol activations? GUI_38
44.How do I save an axes as an image? GUI_39
45.How can I make a simple drawing program? GUI_39
46.How can I set a button's background to match an image? GUI_40
47.How can I save the state of a system of GUIs to use later? GUI_41