{"id":222,"date":"2025-12-19T09:09:40","date_gmt":"2025-12-19T08:09:40","guid":{"rendered":"https:\/\/mrhengineering.dk\/?p=222"},"modified":"2025-12-21T07:53:12","modified_gmt":"2025-12-21T06:53:12","slug":"4k-resolution-in-opencv-with-dell-ultrashap","status":"publish","type":"post","link":"https:\/\/mrhengineering.dk\/?p=222","title":{"rendered":"4K resolution in OpenCV with DELL UltraShap"},"content":{"rendered":"\n<p>I\u2019ve recently been experimenting with computer vision using OpenCV, and for my setup, I\u2019m using a Dell UltraSharp webcam to capture images. Out of the box, OpenCV makes it easy to grab frames at 1920\u00d71080 (Full HD), which is great\u2014but according to the camera\u2019s specifications, it should support 3840\u00d72160 (4K) resolution.<\/p>\n\n\n\n<p>Interestingly, when I tested the webcam with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cheese_(software)\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Cheese_(software)\">Cheese<\/a>, 4K worked perfectly. That means the hardware and drivers support it &#8211; so why not OpenCV?<\/p>\n\n\n\n<p>It turns out that most applications for webcams uses the device driver <a href=\"https:\/\/en.wikipedia.org\/wiki\/Video4Linux\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Video4Linux\">V4L2<\/a> (OpenCV, ffmpeg, gstreamer), when these programs are used on Ubuntu. So, how does one change the settings in V4L2? First step is to list the possible video formats for the device:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>v4l2-ctl --list-formats-ext --device 9<\/code><\/pre>\n\n\n\n<p>The possible formats for the Dell UltraSharp is<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>ioctl: VIDIOC_ENUM_FMT\n\tType: Video Capture\n\t&#91;0]: 'YUYV' (YUYV 4:2:2)\n\t\tSize: Discrete 640x480\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 640x360\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 1280x720\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 1920x1080\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t&#91;1]: 'MJPG' (Motion-JPEG, compressed)\n\t\tSize: Discrete 640x480\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 1280x720\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\t\tInterval: Discrete 0.017s (60.000 fps)\n\t\tSize: Discrete 1920x1080\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\t\tInterval: Discrete 0.017s (60.000 fps)\n\t\tSize: Discrete 2560x1440\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 3840x2160\n\t\t\tInterval: Discrete 0.042s (24.000 fps)\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t&#91;2]: 'NV12' (Y\/UV 4:2:0)\n\t\tSize: Discrete 640x480\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 640x360\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 1280x720\n\t\t\tInterval: Discrete 0.033s (30.000 fps)\n\t\tSize: Discrete 1920x1080\n\t\t\tInterval: Discrete 0.033s (30.000 fps)<\/code><\/pre>\n\n\n\n<p>This indicates that the camera must be switched to MJPG to use the highest resolution. In Python this is done by:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fourcc_mjpg = cv2.VideoWriter_fourcc(*'MJPG')\ncap.set(cv2.CAP_PROP_FOURCC, fourcc_mjpg)<\/code><\/pre>\n\n\n\n<p>And in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int fourcc_mjpg = VideoWriter.fourcc('M','J','P','G');\ncapture.set(Videoio.CAP_PROP_FOURCC, fourcc_mjpg);<\/code><\/pre>\n\n\n\n<p>Here is a complete example in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import cv2\n\n# 1) Open the camera explicitly with the V4L2 backend\ncap = cv2.VideoCapture(9, cv2.CAP_V4L2)\n\nif not cap.isOpened():\n    raise RuntimeError(\"Cannot open camera with V4L2 backend\")\n\n# 2) Request MJPG from the camera\n#    Note: set FOURCC *after* opening the device.\nfourcc_mjpg = cv2.VideoWriter_fourcc(*'MJPG')\nprint (\"MJPG setting:\")\nprint (fourcc_mjpg)\ncap.set(cv2.CAP_PROP_FOURCC, fourcc_mjpg)\n\n# (Optional) Set resolution\/fps to modes that the camera supports in MJPG\ncap.set(cv2.CAP_PROP_FRAME_WIDTH, 3840)\ncap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2160)\ncap.set(cv2.CAP_PROP_FPS, 1)\n\n# 3) Verify what you actually got\napplied_fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))\napplied = \"\".join(&#91;chr((applied_fourcc >> (8 * i)) &amp; 0xFF) for i in range(4)])\nprint(\"Applied FOURCC:\", applied)  # Expect \"MJPG\" if accepted\n\nbackend = int(cap.get(cv2.CAP_PROP_BACKEND))\nprint(\"Backend:\", backend)  # Should match cv2.CAP_V4L2\n\n# Read frames\nwhile True:\n    ok, frame = cap.read()\n    if not ok:\n        print(\"Frame read failed\")\n        break\n    cv2.imshow(\"MJPG stream\", frame)\n    if cv2.waitKey(1) == 27:  # ESC\n        break\n\ncap.release()\ncv2<\/code><\/pre>\n\n\n\n<p>And here is one in Java<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import org.opencv.core.Mat;\nimport org.opencv.imgcodecs.Imgcodecs;\nimport org.opencv.videoio.VideoCapture;\nimport org.opencv.videoio.VideoWriter;\nimport org.opencv.videoio.Videoio;\n\nnu.pattern.OpenCV.loadLocally();\n\nVideoCapture capture = new VideoCapture(9, Videoio.CAP_V4L2);\nassertTrue(capture.isOpened());\n\n\/\/ Set the camera into MJPG because that allows high resolution\nint fourcc_mjpg = VideoWriter.fourcc('M', 'J', 'P', 'G');\ncapture.set(Videoio.CAP_PROP_FOURCC, fourcc_mjpg);\n\n\/\/ Set resolution as high as possible\n\/\/ Dell UltraSharp Webcam 3840 x 2160\ncapture.set(Videoio.CAP_PROP_FRAME_WIDTH, 3840);\ncapture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 2160);\ncapture.set(Videoio.CAP_PROP_FPS, 2);\n\n\/\/ Verify the settings\nint height = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);\nint width = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);\nSystem.out.println(\"Camera started. Width = \"+width+\" Height = \"+height);\nSystem.out.println(\" Backend name: \"+capture.getBackendName());\nSystem.out.println(\" FPS = \" + capture.get(Videoio.CAP_PROP_FPS));\n\n\/\/ This is the variable where the image will be stored\nMat frame = new Mat();\n\n\/\/ Take some pictures to allow the webcam to focus.\nint c = 0;\nwhile (c &lt; 100){\n    if (capture.read(frame)) c++;\n}\n\n\/\/ Save the last image\nImgcodecs.imwrite(\"OpenCVcapture.jpg\", frame);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve recently been experimenting with computer vision using OpenCV, and for my setup, I\u2019m using a Dell UltraSharp webcam to capture images. Out of the box, OpenCV makes it easy to grab frames at 1920\u00d71080 (Full HD), which is great\u2014but according to the camera\u2019s specifications, it should support 3840\u00d72160 (4K) resolution. Interestingly, when I tested [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":226,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[22,19,23,18,21,24,20],"class_list":["post-222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-fourcc","tag-java","tag-mjpg","tag-opencv","tag-resolution","tag-v4l2","tag-webcam"],"_links":{"self":[{"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=222"}],"version-history":[{"count":4,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions\/227"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=\/wp\/v2\/media\/226"}],"wp:attachment":[{"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mrhengineering.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}