我有多個文本文件,只是想只提取符合條件的值,如何在matlab中用條件提取文本中的單詞?
文件看起來像這樣
157.76941498460488, u'id': 1056080, u'image_id': 354282, u'bbox': [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 157.76941498460522, u'id': 1056081, u'image_id': 354282, u'bbox': [176.79054054054052, 241.06582278481014, 16.216216216216246, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 130.89018132056108, u'id': 1056082, u'image_id': 354282, u'bbox': [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'english', u'area': 229.08553456429397, u'class': u'machine printed', u'utf8_string': u'7206', u'image_id': 354282, u'bbox': [447.84940154212785, 338.8799273943157, 15.489338584815993, 14.78988488177692], u'legibility': u'legible', u'id': 1232932}, {u'language': u'english', u'area': 125.41629858832702, u'class': u'machine printed', u'utf8_string': u'HSS', u'image_id': 354282, u'bbox': [465.63345695432395, 333.1362827800334, 10.039386119788142, 12.492427036063997], u'legibility': u'legible', u'id': 1232933}]
我想提取所有bboxs如果utf8_string和輸出庫這樣
bbox = [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637]
bbox1 = [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783]
..etc bbox3 and box4 all the bboxs if its 'utf8_string'and legible
我的代碼
i=imread('image.JPEG');
fid = fopen('text1.txt','r');
C = textscan(fid, '%s','Delimiter','');
fclose(fid);
C = C{:};
box = ~cellfun(@isempty, strfind(C,'bbox'));
output = [C{find(box)}]
我得到的不僅是bbox的整條線。
你爲什麼要指定一個空的分隔符? – excaza